方案一: split public String[] split(String regex) Splits this string around matches of the given regular expression. This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result : { "boo", "and", "foo" } o { "b", "", ":and:f" } Parameters: regex - the delimiting regular expression Returns: the array of strings computed by splitting this string around matches of the given regular expression Throws: PatternSyntaxException - if the regular expression's syntax is invalid Since: 1.4 See Also: Pattern方案二: 只考虑空格所字符串拆开,然后对每个子串调用.trim()方法去掉子串前后的空格。
用StringTokenizer 简单得很 new StringTokenizer(你的字符串 , 分隔符 , false)
while (str.indexOf(" ")>0){ str = str.replaceAll(" "," "); }
String[] ss = str.trim().split(" ");
for (int i=0;i<ss.length;i++) System.out.println(ss[i]);
public class StringSplit { public static void main(String[] args) { String s = "aaa bbb ccc ddd eee "; String []ss = s.split(" +"); for(int i = 0;i<ss.length;i++) System.out.println(ss[i]); } } /*aaa bbb ccc ddd eee*/
呵呵,挺有意思的。那大家在看看这个字符串吧 String str = " aa bbb ccc ddd e ee "; 有的运行结果是:aa bbb ccc ddd e ee Press any key to continue... //注意哦,aa上面还是有空行的,而不是的敲的回车 我的运行结果是: aa bbb ccc ddd e ee Press any key to continue... 没有空行的哦。 希望大家继续讨论有时间加我聊哦[email protected]
split
public String[] split(String regex)
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. The string "boo:and:foo", for example, yields the following results with these expressions: Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
Parameters:
regex - the delimiting regular expression
Returns:
the array of strings computed by splitting this string around matches of the given regular expression
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
Since:
1.4
See Also:
Pattern方案二:
只考虑空格所字符串拆开,然后对每个子串调用.trim()方法去掉子串前后的空格。
StringTokenier功能有限!
public class test{
public static void main(String[] args){
String str="aa bb ccc ddd" ;
String[] string = str.split(" ");
for(int i=0;i<string.length;i++){
if(!string[i].equals("")){
System.out.println(string[i].trim());
}
}
}
}
没人说用的是split(" ")么
while (str.indexOf(" ")>0){
str = str.replaceAll(" "," ");
}
String[] ss = str.trim().split(" ");
for (int i=0;i<ss.length;i++) System.out.println(ss[i]);
public static void main(String[] args) {
String s = "aaa bbb ccc ddd eee ";
String []ss = s.split(" +");
for(int i = 0;i<ss.length;i++)
System.out.println(ss[i]);
}
}
/*aaa
bbb
ccc
ddd
eee*/
String str = " aa bbb ccc ddd e ee ";
有的运行结果是:aa
bbb
ccc
ddd
e
ee
Press any key to continue...
//注意哦,aa上面还是有空行的,而不是的敲的回车
我的运行结果是:
aa
bbb
ccc
ddd
e
ee
Press any key to continue...
没有空行的哦。
希望大家继续讨论有时间加我聊哦[email protected]
至于楼上说的问题是因为split只考虑分隔符在中间的情况,如果是以分隔符开始的,就会出现数组第一个元素为空的现象。可以先用trim()切一下。
我的split(\\s+)不正确么?