5

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

giulio
  • 659
  • 2
  • 8
  • 22
  • 6
    Why there? Your title talks about "at a particular position" but we don't know whether you know that position in terms of a character index or something else. – Jon Skeet Feb 09 '15 at 21:21
  • 3
    If you want to put each pair of two words into a string, it might be easier simply to split it all by whitespace then iterate through and concatenating each pair. – colti Feb 09 '15 at 21:21
  • Ya you need a little more specification on what exactly you're looking for. Do you want to split in the middle, some arbitrary index, or some other rule? Do You want just two items after the split or more based on a rule? – MrJman006 Feb 09 '15 at 21:24
  • Unclear what you're asking. – David Conrad Feb 09 '15 at 21:26
  • If you do want just two items you can use substring like so, String split1 = s.substring(0, Index); String split2 = s.substring(Index); where s = "word1 word2 word3 word4" – MrJman006 Feb 09 '15 at 21:27
  • sorry for the confusion, I clarified the question – giulio Feb 09 '15 at 21:28
  • possible duplicate of [Extracting pairs of words using String.split()](http://stackoverflow.com/questions/16485687/extracting-pairs-of-words-using-string-split) – mbomb007 Feb 09 '15 at 21:32

6 Answers6

6

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"

Einar Sundgren
  • 4,325
  • 9
  • 40
  • 59
3

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]
Community
  • 1
  • 1
ThisClark
  • 14,352
  • 10
  • 69
  • 100
2
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.

  • 1
    I would suggest to use the String.indexOf("foo"); instead. – Einar Sundgren Feb 09 '15 at 21:32
  • String.indexOf("foo") only returns the first occurrence of the character. You'd have to do something like String.indexOf("foo", 5) where the second parameter is the index that indexOf() will start searching from. –  Feb 09 '15 at 21:36
1

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] + " ";
    }
}
austin wernli
  • 1,801
  • 12
  • 15
0

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(" ")));
0

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]
    }
sammarcow
  • 2,736
  • 2
  • 28
  • 56