Reputation: 659
Assume you have a string of the form "word1 word2 word3 word4"
. What is the simplest way to split it such that split[0] = "word1 word2"
and split[1] = "word3 word4"
?
EDIT: Clarifying
I want to split such that instead of having split[0] = "word1", I have the first 2 words (I agree it was not clear) and all the other words in split[1], ie on the second space
Upvotes: 5
Views: 26712
Reputation: 4423
I would use the String.substring(beginIndex, endIndex); and String.substring(beginIndex);
String a = "word1 word2 word3 word4";
int first = a.indexOf(" ");
int second = a.indexOf(" ", first + 1);
String b = a.substring(0,second);
String c = b.subString(second); // Only startindex, cuts at the end of the string
This would result in a = "word1 word2" and b = "word3 word4"
Upvotes: 6
Reputation: 2956
Split the string by its common delimiter (spaces in this case) and conditionally re-add the delimiter of choice in output, iterating by 2
string original = "word1 word2 word3 word4";
string[] delimitedSplit = original.split(" ");
for (int i = 0; i< delimitedSplit.length; i+=2) {
if (i < delimitedSplit.length - 1 ) { //handle uneven pairs
out.println(delimitedSplit[i] + " " + delimitedSplit[i+1] );
}
else {
out.println(delimitedSplit[i]
}
Upvotes: 0
Reputation: 1
If you would want to split a string into sets of two words, this code could help:
String toBeSplit = "word1 word2 word3 word4";
String firstSplit = a.substr(0,tBS.indexOf(" ", tBS.indexOf(" ")));
String secondSplit = firstSplit.substr(tBS.indexOf(" ", tBS.indexOf(" ")));
Upvotes: 0
Reputation: 1801
This should do what you want to achieve.
You can use String.split(" ");
to split on spaces in the initial string.
Then from there you said you wanted the first two words in split[0]
so i just handled that with a simple conditional if(i==0 || i == 1) add it to split[0]
String word = "word1 word2 word3 word4";
String[] split = new String[2];
//Split the initial string on spaces which will give you an array of the words.
String[] wordSplit = word.split(" ");
//Foreach item wordSplit add it to either Split[0] or Split[1]
for (int i = 0; i < wordSplit.length(); i++) {
//Determine which position to add the string to
if (i == 0 || i == 1) split[0] += wordSplit[i] + " ";
else {
split[1] += wordSplit[i] + " ";
}
}
Upvotes: 1
Reputation: 14823
Do you want to do this in pairs always? This is a dynamic solution provided by the SO community wiki, Extracting pairs of words using String.split()
String input = "word1 word2 word3 word4";
String[] pairs = input.split("(?<!\\G\\w+)\\s");
System.out.println(Arrays.toString(pairs));
output:
[word1 word2, word3 word4]
Upvotes: 3
Reputation:
String str = "word1 word2 word3 word4";
String subStr1 = str.substring(0,12);
String subStr2 = str.substring(12);
Would be your best bet for splitting at a position. If you need to split on the second occurrence of space, a for loop might be a better option.
int count = 0;
int splitIndex;
for (int i = 0; i < str.length(); i++){
if(str.charAt(i) == " "){
count++;
}
if (count == 2){
splitIndex = i;
}
}
and then you would split it into substrings as above.
Upvotes: 2