简体   繁体   English

Java String split方法被2个或更多空格分隔

[英]Java String split method split by 2 or more spaces

How can I split a string by "2 or more spaces" only using the java split method 如何仅使用java split方法将字符串分割为“ 2个或更多空格”

An example: 一个例子:

"cat dog    horse   elephant"

will be splitted into: 将分为:

cat dog
horse
elephant

Thanks. 谢谢。

You need to split the String using following regex 您需要使用以下regex拆分String

\\s{2,}

This will split the string by 2 or more spaces 这会将字符串分割2个或更多空格

String str = "cat dog    horse   elephant";
String[] parts = str.split("\\s{2,}");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM