String的split小记

public class SplitDemo {
    public static void main(String[] args) {
        String a = "abcooob";
        String[] as = a.split("o");
        System.out.println(as.length);
    }
}

运行结果是
html

4
abc            b
java

由于分割成{“abc”,"","","b"}的值,这个正常理解。正则表达式

public class SplitDemo {
    public static void main(String[] args) {
        String a = "abcooo";
        String[] as = a.split("o");
        System.out.println(as.length);
        for (String string : as) {
            System.out.print(string+"\t");
        }
    }
}

这个运行结果是:api

1
abc   
---------------------------------------------------------数组

为何呢?ide

看api
spa

wKiom1dma-qwCGyIAAA1RTx7M2g621.png-wh_50


split

public String[] split(String regex)
  • 根据给定正则表达式的匹配拆分此字符串。code

    该方法的做用就像是使用给定的表达式和限制参数 0 来调用两参数 split 方法。所以,所得数组中不包括结尾空字符串。htm

    例如,字符串 "boo:and:foo" 使用这些表达式可生成如下结果:blog

    Regex 结果
    : { "boo", "and", "foo" }
    o { "b", "", ":and:f" }

    • 参数:

    • regex - 定界正则表达式

    • 返回:

    • 字符串数组,它是根据给定正则表达式的匹配拆分此字符串肯定的

    • 抛出:

    • PatternSyntaxException - 若是正则表达式的语法无效



----------------------------------------------------------------------------------

结论:split分割所得数组中不包括结尾空字符串。

----------------------------------------------------------------------------------

相关文章
相关标签/搜索