I have a string with several words separated by spaces, e.g. "firstword second third", and an ArrayList. I want to split the string into several pieces, and add the 'piece' strings to the ArrayList.
我有一个字符串,其中有几个单词用空格分隔,例如“firstword second third”和一个ArrayList。我想将字符串拆分成几个部分,并将'piece'字符串添加到ArrayList。
For example,"firstword second third" can be split to three separate strings , so the ArrayList would have 3 elements; "1 2 3 4" can be split into 4 strings, in 4 elements of the ArrayList. See the code below:
例如,“firstword second third”可以拆分为三个单独的字符串,因此ArrayList将有3个元素; “1 2 3 4”可以分成4个字符串,在ArrayList的4个元素中。请参阅以下代码:
public void separateAndAdd(String notseparated) {
for(int i=0;i<canBeSepartedinto(notseparated);i++{
//what should i put here in order to split the string via spaces?
thearray.add(separatedstring);
}
}
public int canBeSeparatedinto(String string)
//what do i put here to find out the amount of spaces inside the string?
return ....
}
Please leave a comment if you dont get what I mean or I should fix some errors in this post. Thanks for your time!
如果你不理解我的意思,请发表评论或者我应该在这篇文章中修正一些错误。谢谢你的时间!
7 个解决方案
#1
10
You can split the String at the spaces using split()
:
您可以使用split()在空格处拆分String:
String[] parts = inputString.split(" ");
Afterwards iterate over the array and add the individual parts (if !"".equals(parts[i]
) to the list.
然后迭代数组并将各个部分(如果!“”。equals(parts [i])添加到列表中。
#2
4
If you want to split on one space, you can use .split(" ");
. If you want to split on all spaces in a row, use .split(" +");
.
如果要在一个空格上拆分,可以使用.split(“”);.如果要拆分连续的所有空格,请使用.split(“+”);.
Consider the following example:
请考虑以下示例:
class SplitTest {
public static void main(String...args) {
String s = "This is a test"; // note two spaces between 'a' and 'test'
String[] a = s.split(" ");
String[] b = s.split(" +");
System.out.println("a: " + a.length);
for(int i = 0; i < a.length; i++) {
System.out.println("i " + a[i]);
}
System.out.println("b: " + b.length);
for(int i = 0; i < b.length; i++) {
System.out.println("i " + b[i]);
}
}
}
If you are worried about non-standard spaces, you can use "\\s+"
instead of " +"
, as "\\s"
will capture any white space, not just the 'space character'.
如果您担心非标准空格,可以使用“\\ s +”而不是“+”,因为“\\ s”将捕获任何空白区域,而不仅仅是“空格字符”。
So your separate and add method becomes:
所以你的单独和添加方法变成:
void separateAndAdd(String raw) {
String[] tokens = raw.split("\\s+");
theArray.ensureCapacity(theArray.size() + tokens.length); // prevent unnecessary resizes
for(String s : tokens) {
theArray.add(s);
}
}
Here's a more complete example - note that there is a small modification in the separateAndAdd
method that I discovered during testing.
这是一个更完整的示例 - 请注意,我在测试期间发现的separateAndAdd方法中有一个小的修改。
import java.util.*;
class SplitTest {
public static void main(String...args) {
SplitTest st = new SplitTest();
st.separateAndAdd("This is a test");
st.separateAndAdd("of the emergency");
st.separateAndAdd("");
st.separateAndAdd("broadcast system.");
System.out.println(st);
}
ArrayList<String> theArray = new ArrayList<String>();
void separateAndAdd(String raw) {
String[] tokens = raw.split("\\s+");
theArray.ensureCapacity(theArray.size() + tokens.length); // prevent unnecessary resizes
for(String s : tokens) {
if(!s.isEmpty()) theArray.add(s);
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
for(String s : theArray)
sb.append(s).append(" ");
return sb.toString().trim();
}
}
#3
2
Do this:
thearray = new ArrayList<String>(Arrays.asList(notseparated.split(" ")));
or if thearray
already instantiated
或者如果thearray已经实例化了
thearray.addAll(Arrays.asList(notseparated.split(" ")));
#4
2
I would suggest using the
我建议使用
apache.commons.lang.StringUtils library.
It is the easiest and covers all the different conditions you can want int he spliting up of a string with minimum code.
它是最简单的,涵盖了你可以想要的所有不同条件,用最少的代码分割字符串。
Here is a reference to the split method : Split Method
以下是拆分方法的参考:拆分方法
you can also refer to the other options available for the split method on the same link.
您还可以参考同一链接上可用于拆分方法的其他选项。
#5
1
You can do that using .split()
try this
你可以使用.split()试试这个
String[] words= inputString.split("\\s");
#6
1
If you want to split the string in different parts, like here i am going to show you that how i can split this string 14-03-2016 in day,month and year.
如果你想在不同的部分拆分字符串,就像在这里我将告诉你如何在日,月和年中分割这个字符串14-03-2016。
String[] parts = myDate.split("-");
day=parts[0];
month=parts[1];
year=parts[2];
#7
0
try this: string to 2 part:
试试这个:字符串为2部分:
public String[] get(String s){
int l = s.length();
int t = l / 2;
String first = "";
String sec = "";
for(int i =0; i<l; i++){
if(i < t){
first += s.charAt(i);
}else{
sec += s.charAt(i);
}
}
String[] result = {first, sec};
return result;
}
example:
String s = "HelloWorld";
String[] res = get(s);
System.out.println(res[0]+" "+res[1])
output:
Hello World
#1
10
You can split the String at the spaces using split()
:
您可以使用split()在空格处拆分String:
String[] parts = inputString.split(" ");
Afterwards iterate over the array and add the individual parts (if !"".equals(parts[i]
) to the list.
然后迭代数组并将各个部分(如果!“”。equals(parts [i])添加到列表中。
#2
4
If you want to split on one space, you can use .split(" ");
. If you want to split on all spaces in a row, use .split(" +");
.
如果要在一个空格上拆分,可以使用.split(“”);.如果要拆分连续的所有空格,请使用.split(“+”);.
Consider the following example:
请考虑以下示例:
class SplitTest {
public static void main(String...args) {
String s = "This is a test"; // note two spaces between 'a' and 'test'
String[] a = s.split(" ");
String[] b = s.split(" +");
System.out.println("a: " + a.length);
for(int i = 0; i < a.length; i++) {
System.out.println("i " + a[i]);
}
System.out.println("b: " + b.length);
for(int i = 0; i < b.length; i++) {
System.out.println("i " + b[i]);
}
}
}
If you are worried about non-standard spaces, you can use "\\s+"
instead of " +"
, as "\\s"
will capture any white space, not just the 'space character'.
如果您担心非标准空格,可以使用“\\ s +”而不是“+”,因为“\\ s”将捕获任何空白区域,而不仅仅是“空格字符”。
So your separate and add method becomes:
所以你的单独和添加方法变成:
void separateAndAdd(String raw) {
String[] tokens = raw.split("\\s+");
theArray.ensureCapacity(theArray.size() + tokens.length); // prevent unnecessary resizes
for(String s : tokens) {
theArray.add(s);
}
}
Here's a more complete example - note that there is a small modification in the separateAndAdd
method that I discovered during testing.
这是一个更完整的示例 - 请注意,我在测试期间发现的separateAndAdd方法中有一个小的修改。
import java.util.*;
class SplitTest {
public static void main(String...args) {
SplitTest st = new SplitTest();
st.separateAndAdd("This is a test");
st.separateAndAdd("of the emergency");
st.separateAndAdd("");
st.separateAndAdd("broadcast system.");
System.out.println(st);
}
ArrayList<String> theArray = new ArrayList<String>();
void separateAndAdd(String raw) {
String[] tokens = raw.split("\\s+");
theArray.ensureCapacity(theArray.size() + tokens.length); // prevent unnecessary resizes
for(String s : tokens) {
if(!s.isEmpty()) theArray.add(s);
}
}
public String toString() {
StringBuilder sb = new StringBuilder();
for(String s : theArray)
sb.append(s).append(" ");
return sb.toString().trim();
}
}
#3
2
Do this:
thearray = new ArrayList<String>(Arrays.asList(notseparated.split(" ")));
or if thearray
already instantiated
或者如果thearray已经实例化了
thearray.addAll(Arrays.asList(notseparated.split(" ")));
#4
2
I would suggest using the
我建议使用
apache.commons.lang.StringUtils library.
It is the easiest and covers all the different conditions you can want int he spliting up of a string with minimum code.
它是最简单的,涵盖了你可以想要的所有不同条件,用最少的代码分割字符串。
Here is a reference to the split method : Split Method
以下是拆分方法的参考:拆分方法
you can also refer to the other options available for the split method on the same link.
您还可以参考同一链接上可用于拆分方法的其他选项。
#5
1
You can do that using .split()
try this
你可以使用.split()试试这个
String[] words= inputString.split("\\s");
#6
1
If you want to split the string in different parts, like here i am going to show you that how i can split this string 14-03-2016 in day,month and year.
如果你想在不同的部分拆分字符串,就像在这里我将告诉你如何在日,月和年中分割这个字符串14-03-2016。
String[] parts = myDate.split("-");
day=parts[0];
month=parts[1];
year=parts[2];
#7
0
try this: string to 2 part:
试试这个:字符串为2部分:
public String[] get(String s){
int l = s.length();
int t = l / 2;
String first = "";
String sec = "";
for(int i =0; i<l; i++){
if(i < t){
first += s.charAt(i);
}else{
sec += s.charAt(i);
}
}
String[] result = {first, sec};
return result;
}
example:
String s = "HelloWorld";
String[] res = get(s);
System.out.println(res[0]+" "+res[1])
output:
Hello World