我想知道为什么拆分字符串的结果
foo:and:boo
正则表达式
o
里面有空格吗? 所以输出看起来像这样 -
f "" and b
有人可以解释为什么“”在这里吗?谢谢!
最佳答案
Can someone explain why "" is here?
因为 foo
中的 o
和 o
之间没有任何内容。正则表达式 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 + "]");
}
}
}
输出:
Results from using just "o": [f] [] [:and:b] Results from using "o+": [f] [:and:b]
关于Java 字符串拆分问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32579857/