我需要用空格分割我的字符串。 为此我试过:
str = "Hello I'm your String";
String[] splited = str.split(" ");
但这似乎并不奏效。
我需要用空格分割我的字符串。 为此我试过:
str = "Hello I'm your String";
String[] splited = str.split(" ");
但这似乎并不奏效。
当前回答
试试这个
String str = "This is String";
String[] splited = str.split("\\s+");
String split_one=splited[0];
String split_second=splited[1];
String split_three=splited[2];
Log.d("Splited String ", "Splited String" + split_one+split_second+split_three);
其他回答
不仅是空白,我的解决方案也解决了看不见的字符。
str = "Hello I'm your String";
String[] splited = str.split("\p{Z}");
将解决方案整合在一起!
public String getFirstNameFromFullName(String fullName){
int indexString = fullName.trim().lastIndexOf(' ');
return (indexString != -1) ? fullName.trim().split("\\s+")[0].toUpperCase() : fullName.toUpperCase();
}
如果你不想使用String split方法,那么你可以使用Java中的StringTokenizer类作为..
StringTokenizer tokens = new StringTokenizer("Hello I'm your String", " ");
String[] splited = new String[tokens.countTokens()];
int index = 0;
while(tokens.hasMoreTokens()){
splited[index] = tokens.nextToken();
++index;
}
Try
String[] splited = str.split("\\s");
http://download.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html
使用Stringutils.split()按白色步长分割字符串。例如StringUtils。split("Hello World")返回"Hello"和"World";
为了解决上述情况,我们采用了这样的分割方法
String split[]= StringUtils.split("Hello I'm your String");
当我们打印拆分数组时,输出将是:
你好
I'm
your
字符串
完整的示例演示检查这里