在我的字符串中,我想基于两个或多个空格来分割字符串或标记化字符串。
防爆
string = "I am Chaitanya Gadam. Split this srting."
我想输出为-
str[0]= "I am" str[1]= "Chaitanya Gadam." str[2]= "Split" str[3]= "this string."
string.split("\\s\\s+");
(要么)
string.split("\\s{2,}"); String string = "I am Chaitanya Gadam. Split this srting."; String[] str = string.split("\\s{2,}"); for(String s: str) { System.out.println(s+":"+s.length()); } System.out.println(Arrays.toString(string.split("\\s\\s+"))); Output: I am:4 Chaitanya Gadam.:16 Split:5 this srting.:12 [I am, Chaitanya Gadam., Split, this srting.]
分割两个或多个空格作为分隔符的字符串。在正则表达式中使用转义字符可以提高可读性,而不是使用空格字符。