I need to split my String by spaces. For this I tried:
我需要用空格分割字符串。我试着:
str = "Hello I'm your String";
String[] splited = str.split(" ");
But it doesn't seems to work.
但这似乎行不通。
12 个解决方案
#1
424
What you have should work. If, however, the spaces provided are defaulting to... something else? You can use the whitespace regex:
你所拥有的应该是有用的。但是,如果提供的空间默认为……别的吗?您可以使用空格regex:
str = "Hello I'm your String";
String[] splited = str.split("\\s+");
This will cause any number of consecutive spaces to split your string into tokens.
这将导致任意数量的连续空格将字符串分割为令牌。
As a side note, I'm not sure "splited" is a word :) I believe the state of being the victim of a split is also "split". It's one of those tricky grammar things :-) Not trying to be picky, just figured I'd pass it on!
顺便说一句,我不确定“分裂”是一个词:)我认为成为分裂受害者的状态也是“分裂”。这是一件棘手的语法问题:- - -不要太挑剔,我只是想把它传下去!
#2
41
While the accepted answer is good, be aware that you will end up with a leading empty string if your input string starts with a white space. For example, with:
虽然所接受的答案是好的,但是请注意,如果输入字符串以空格开头,那么您将得到一个前导空字符串。例如,使用:
String str = " Hello I'm your String";
String[] splitStr = str.split("\\s+");
The result will be:
结果将是:
splitStr[0] == "";
splitStr[1] == "Hello";
splitStr[2] == "I'm";
splitStr[3] == "Your";
splitStr[4] == "String";
So you might want to trim your string before splitting it:
因此,你可能想要修剪你的线之前,分裂它:
String str = " Hello I'm your String";
String[] splitStr = str.trim().split("\\s+");
#3
23
I do believe that putting a regular expression in the str.split parentheses should solve the issue. The Java String.split() method is based upon regular expressions so what you need is:
我相信在string .split圆括号中放入正则表达式应该可以解决这个问题。Java String.split()方法基于正则表达式,因此需要:
str = "Hello I'm your String";
String[] splitStr = str.split("\\s+");
#4
12
Use Stringutils.split()
to split the string by whites paces. For example StringUtils.split("Hello World")
returns "Hello" and "World";
使用Stringutils.split()将字符串分割成白色。例如stringutil的。split(“Hello World”)返回“Hello”和“World”;
In order to solve the mentioned case we use split method like this
为了解决上述情况,我们采用了这样的分割方法
String split[]= StringUtils.split("Hello I'm your String");
when we print the split array the output will be :
当我们打印分割数组时,输出将是:
Hello
你好
I'm
我
your
你的
String
字符串
For complete example demo check here
关于完整的示例演示,请在这里检查
#5
7
Try this one
试试这个
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);
#6
4
if somehow you don't wanna use String split method then you can use StringTokenizer class in Java as..
如果不希望使用字符串分割方法,那么可以在Java中使用StringTokenizer类as。
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;
}
#7
3
An alternative way would be:
另一种办法是:
import java.util.regex.Pattern;
...
private static final Pattern SPACE = Pattern.compile(" ");
String[] arr = SPACE.split(str); // str is the string to be split
Saw it here
看到这里
#8
2
Try
试一试
String[] splited = str.split("\\s");
http://download.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html
http://download.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html
#9
1
OK, so we have to do splitting as you already got the answer I would generalize it.
好的,我们要做分裂,因为你们已经得到了我要推广的答案。
If you want to split any string by spaces, delimiter(special chars).
如果要按空格分隔任何字符串,分隔符(特殊字符)。
First, remove the leading space as they create most of the issues.
首先,删除领导空间,因为它们创建了大多数问题。
str1 = " Hello I'm your String ";
str2 = " Are you serious about this question_ boy, aren't you? ";
First remove the leading space which can be space, tab etc.
首先删除可以是空格、制表符等的前导空格。
String s = str1.replaceAll("^\\s+","");//starting with whitespace one or more
Now if you want to split by space or any special char.
现在,如果你想按空格或任何特殊字符来划分。
String[] sa = s.split("[^\\w]+");//split by any non word char
But as w contains [a-zA-Z_0-9] ,so if you want to split by underscore(_) also use
但是由于w包含[a-zA-Z_0-9],所以如果您想要通过下划线(_)进行拆分,也可以使用。
String[] sa = s.split("[!,? ._'@]+");//for str2 after removing leading space
#10
0
Simple to Spit String by Space
简单地按空格吐字串
String CurrentString = "First Second Last";
String[] separated = CurrentString.split(" ");
for (int i = 0; i < separated.length; i++) {
if (i == 0) {
Log.d("FName ** ", "" + separated[0].trim() + "\n ");
} else if (i == 1) {
Log.d("MName ** ", "" + separated[1].trim() + "\n ");
} else if (i == 2) {
Log.d("LName ** ", "" + separated[2].trim());
}
}
#11
0
Here is a method to trim a String that has a "," or white space
这里有一种方法来修剪带有“,”或空格的字符串
private String shorterName(String s){
String[] sArr = s.split("\\,|\\s+");
String output = sArr[0];
return output;
}
#12
0
you can saperate string using the below code
可以使用下面的代码分隔字符串
String thisString="Hello world";
String[] parts = theString.split(" ");
String first = parts[0];//"hello"
String second = parts[1];//"World"
#1
424
What you have should work. If, however, the spaces provided are defaulting to... something else? You can use the whitespace regex:
你所拥有的应该是有用的。但是,如果提供的空间默认为……别的吗?您可以使用空格regex:
str = "Hello I'm your String";
String[] splited = str.split("\\s+");
This will cause any number of consecutive spaces to split your string into tokens.
这将导致任意数量的连续空格将字符串分割为令牌。
As a side note, I'm not sure "splited" is a word :) I believe the state of being the victim of a split is also "split". It's one of those tricky grammar things :-) Not trying to be picky, just figured I'd pass it on!
顺便说一句,我不确定“分裂”是一个词:)我认为成为分裂受害者的状态也是“分裂”。这是一件棘手的语法问题:- - -不要太挑剔,我只是想把它传下去!
#2
41
While the accepted answer is good, be aware that you will end up with a leading empty string if your input string starts with a white space. For example, with:
虽然所接受的答案是好的,但是请注意,如果输入字符串以空格开头,那么您将得到一个前导空字符串。例如,使用:
String str = " Hello I'm your String";
String[] splitStr = str.split("\\s+");
The result will be:
结果将是:
splitStr[0] == "";
splitStr[1] == "Hello";
splitStr[2] == "I'm";
splitStr[3] == "Your";
splitStr[4] == "String";
So you might want to trim your string before splitting it:
因此,你可能想要修剪你的线之前,分裂它:
String str = " Hello I'm your String";
String[] splitStr = str.trim().split("\\s+");
#3
23
I do believe that putting a regular expression in the str.split parentheses should solve the issue. The Java String.split() method is based upon regular expressions so what you need is:
我相信在string .split圆括号中放入正则表达式应该可以解决这个问题。Java String.split()方法基于正则表达式,因此需要:
str = "Hello I'm your String";
String[] splitStr = str.split("\\s+");
#4
12
Use Stringutils.split()
to split the string by whites paces. For example StringUtils.split("Hello World")
returns "Hello" and "World";
使用Stringutils.split()将字符串分割成白色。例如stringutil的。split(“Hello World”)返回“Hello”和“World”;
In order to solve the mentioned case we use split method like this
为了解决上述情况,我们采用了这样的分割方法
String split[]= StringUtils.split("Hello I'm your String");
when we print the split array the output will be :
当我们打印分割数组时,输出将是:
Hello
你好
I'm
我
your
你的
String
字符串
For complete example demo check here
关于完整的示例演示,请在这里检查
#5
7
Try this one
试试这个
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);
#6
4
if somehow you don't wanna use String split method then you can use StringTokenizer class in Java as..
如果不希望使用字符串分割方法,那么可以在Java中使用StringTokenizer类as。
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;
}
#7
3
An alternative way would be:
另一种办法是:
import java.util.regex.Pattern;
...
private static final Pattern SPACE = Pattern.compile(" ");
String[] arr = SPACE.split(str); // str is the string to be split
Saw it here
看到这里
#8
2
Try
试一试
String[] splited = str.split("\\s");
http://download.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html
http://download.oracle.com/javase/tutorial/essential/regex/pre_char_classes.html
#9
1
OK, so we have to do splitting as you already got the answer I would generalize it.
好的,我们要做分裂,因为你们已经得到了我要推广的答案。
If you want to split any string by spaces, delimiter(special chars).
如果要按空格分隔任何字符串,分隔符(特殊字符)。
First, remove the leading space as they create most of the issues.
首先,删除领导空间,因为它们创建了大多数问题。
str1 = " Hello I'm your String ";
str2 = " Are you serious about this question_ boy, aren't you? ";
First remove the leading space which can be space, tab etc.
首先删除可以是空格、制表符等的前导空格。
String s = str1.replaceAll("^\\s+","");//starting with whitespace one or more
Now if you want to split by space or any special char.
现在,如果你想按空格或任何特殊字符来划分。
String[] sa = s.split("[^\\w]+");//split by any non word char
But as w contains [a-zA-Z_0-9] ,so if you want to split by underscore(_) also use
但是由于w包含[a-zA-Z_0-9],所以如果您想要通过下划线(_)进行拆分,也可以使用。
String[] sa = s.split("[!,? ._'@]+");//for str2 after removing leading space
#10
0
Simple to Spit String by Space
简单地按空格吐字串
String CurrentString = "First Second Last";
String[] separated = CurrentString.split(" ");
for (int i = 0; i < separated.length; i++) {
if (i == 0) {
Log.d("FName ** ", "" + separated[0].trim() + "\n ");
} else if (i == 1) {
Log.d("MName ** ", "" + separated[1].trim() + "\n ");
} else if (i == 2) {
Log.d("LName ** ", "" + separated[2].trim());
}
}
#11
0
Here is a method to trim a String that has a "," or white space
这里有一种方法来修剪带有“,”或空格的字符串
private String shorterName(String s){
String[] sArr = s.split("\\,|\\s+");
String output = sArr[0];
return output;
}
#12
0
you can saperate string using the below code
可以使用下面的代码分隔字符串
String thisString="Hello world";
String[] parts = theString.split(" ");
String first = parts[0];//"hello"
String second = parts[1];//"World"