public class Test {
public static void main(String[] args) {
String s = "hello boy ";
System.out.println(s.split(" ").length);
String ss = " hello boy ";
System.out.println(ss.split(" ").length);
System.out.println(ss.split(" ")[0].equals(""));
}
}
运行结果:
2
3
true
split(“ ”)方法在分割字符串时,若字符串的第一位置是空则分割后第一个位置的结果为空的字符串,其长度为零但占据一个字符串的位置。