split()方法分割字符串

来源:互联网 发布:sql语句添加字段例子 编辑:程序博客网 时间:2024/04/29 15:49
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(“ ”)方法在分割字符串时,若字符串的第一位置是空则分割后第一个位置的结果为空的字符串,其长度为零但占据一个字符串的位置。

原创粉丝点击