有人可以解释为什么""在这里?
其原因在于之间没有任何东西o和o在foo.正则表达式o分裂o字符串中的每个个体.
如果您使用过o+,那么您就不会拥有它,""因为您说的是"拆分一个或多个 o s":
class Example
{
public static void main(String[] args)
{
String str = "foo:and:boo";
test("Results from using just \"o\":", str, "o");
test("Results from using \"o+\":", str, "o+");
}
private static void test(String label, String str, String rex)
{
String[] results = str.split(rex);
System.out.println(label);
for (String result : results) {
System.out.println("[" + result + "]");
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Results from using just "o": [f] [] [:and:b] Results from using "o+": [f] [:and:b]