且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java:当单词之间有可变数量的空格时,按空格分割字符串?

更新时间:2022-12-27 17:53:26

使用 \\\\ + 即使它们更多,也要拆分空格。

Use \\s+ to split on spaces even if they are more.

String string = "1-50  of 500+";
String[] stringArray = string.split("\\s+");

for (String str : stringArray)
{
    System.out.println(str);
}

完整示例: http://ideone.com/CFVr6N

编辑:

如果您还要拆分选项卡,请将正则表达式更改为 \\\\ + | \\t + 并且它还检测空格和制表符。

If you also want to split on tabs, change the regex to \\s+|\\t+ and it detects both spaces and tabs as well.