2

The first round of this was answered here last night. The solution that @Reimeus gave was great and gives the below output from:

My name is the mighty llama

To:

String[] stringArray = string.split("(?<!\\G\\S+)\\s");

My name 
is the
mighty llama

However, I now find myself need to correct what I'm doing. I actually need to be able to split the string like so.

My name
name is
is the
the mighty
mighty llama
Community
  • 1
  • 1
TheMightyLlama
  • 1,243
  • 1
  • 19
  • 51
  • 1
    Could you not split it into single words and build your required array in the code? – Szymon Dec 07 '13 at 10:04
  • 1
    Have you tried anything or are you outsourcing your work to SO? – user93353 Dec 07 '13 at 10:06
  • @Szymon I could, in fact I did. However because I'm doing this for around 1000 articles within a ten minute window I wanted to avoid looping through the articles. As I'm not familiar with Regex i'm asking you guys. – TheMightyLlama Dec 07 '13 at 10:12
  • Regex is not magic either and it also takes time to process it. The problem with splitting is that you would have to go back to an already split off part. – Szymon Dec 07 '13 at 10:24

2 Answers2

5

You can split it into single words and build your required array in the code.

String string = "My name is the mighty llama";
String[] stringArray = string.split(" ");
String[] outputArray = new String[stringArray.length - 1];
for (int i = 0; i < stringArray.length - 1; i++) {
    outputArray[i] = stringArray[i] + " " + stringArray[i+1];
}
Szymon
  • 42,577
  • 16
  • 96
  • 114
1

you cannot duplicate a token with String.split(), because regex is used to SEARCH.

you cannot do this without some kind of edit, maybe a String.replaceAll() will accomplish.

Michele Mariotti
  • 7,372
  • 5
  • 41
  • 73