【发布时间】:2012-04-22 05:23:34
【问题描述】:
当我在python中拆分字符串时,相邻的空格分隔符被合并:
>>> str = "hi there"
>>> str.split()
['hi', 'there']
在 Java 中,分隔符不合并:
$ cat Split.java
class Split {
public static void main(String args[]) {
String str = "hi there";
String result = "";
for (String tok : str.split(" "))
result += tok + ",";
System.out.println(result);
}
}
$ javac Split.java ; java Split
hi,,,,,,,,,,,,,,there,
有没有一种直接的方法可以在 java 中获取 python 空间分割语义?
【问题讨论】:
标签: java python regex split spaces