25
2016
11

java字符串格式化-String.format方法

界面开发中经常要用到显示时间,当前时间,正计时,倒计时,视频总时长,播放时长等等。

时间要以一定的格式显示,比如只显示分和秒00:00。

为了减少重复性劳动,自己封装了一个方法。

25
2016
11

java中的split方法

转行做安卓开发了,最近一直在学习java。

从服务器获取了一个字符串,想用split方法把字符串分割成数组。


split定义如下:

String[] java.lang.String.split(String regex)

结果发现,如果是这样

String s="a;0;;;";
String[] s1=s.split(";");
System.out.println("s1.length="+s1.length);
for(int i=0;i<s1.length;i++){
System.out.println(i+":"+s1[i]);
}

输出结果:

s1.length=2
0:a
1:0

长度是2,只分割出了a和0,后边的都被忽略了。

如果字符串改成这样:String s="a;0;;;b";

输出结果为:

s1.length=5
0:a
1:0
2:
3:
4:b

也就是说后边连续的空字符串会被忽略掉。

帮助中是这么描述的:

  • Splits this string around matches of the given regular expression.

    This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array

尾部的空字符串是不包含在返回数组中的。

没看源码具体是怎么实现的,感觉很反人类。

虽然可以认为的在最后加一个分隔符和一个任意字符,然后分割,再把数组的最后一项去掉,就像这样。

String s="a;0;;;";
s=s+";b";
String[] s1=s.split(";");
System.out.println("s1.length="+s1.length);
for(int i=0;i<s1.length;i++){
System.out.println(i+":"+s1[i]);
}

输出变成了:

s1.length=6
0:a
1:0
2:
3:
4:
5:b

把最后一项去掉,就是想要的结果了。

但是这种方法毕竟非主流。

甚至想到了自己写一个split方法,想了想,太难了,写不好。

后来发现,还有一个两个参数的split方法,定义如下:

String[] java.lang.String.split(String regex, int limit)
  • Splits this string around matches of the given regular expression.

    The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

    The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded. 

看不懂,大概是说,第二个参数可以指定最大的分割次数,也就是分割的段数。

但是我们一开始并不知道字符串应该被分割成几段,难道还要先去查一下里边有多少个分割符?

其实不用,我们可以传一个很大的数,远大于分隔符的个数就可以了。

或者可以直接传一个负数,这样,也会尽可能多的分割。

String s="a;0;;;";
String[] s0=s.split(";", -1);
System.out.println("s0.length="+s0.length);
for(int i=0;i<s0.length;i++){
System.out.println(i+":"+s0[i]);
}

输出结果是:

s0.length=5
0:a
1:0
2:
3:
4:

两个分号之间的,会得到空字符串,正是我们想要的结果。

31
2015
01

java学习(一)

最近在学java,看了看《thinking in java》,写的很好,只是写得太基础了,太难懂,等入了门可以多翻翻,深入理解java的机制。

用惯了as,感觉c,c++之类的太难学了,太基础,什么都得自己考虑到,java介于两者之间,刚开始可以用一些顶层封装好的类,等水平提高了再深入去理解底层的东西。

============================================================

1、第一个窗口程序;

参考:JAVA之窗口和布局

«1»