2

I need to split a string into pairs of characters, but where the pairs overlap. For example, I have a string Testing, and I want the output:

[Te, es, st, ti, in, ng]

I tried some things with String.split but I am not getting the right output:

String s = "Testing";
System.out.println(java.util.Arrays.toString(s.split("(?<=\\G..)")));

That gives me:

[Te, st, in, g]

Can anyone help me?

Boann
  • 47,128
  • 13
  • 114
  • 141
Sanket990
  • 655
  • 1
  • 9
  • 22

2 Answers2

4

split simply searches for places to split, it can't add anything new to result. In other words you can't split like

"abc"` -> "ab", "bc"

because b can be part of only one result.

What you can do is use Pattern/Matcher combo which will find and consume one character at a time, and will store somewhere second one without consuming it (so we need here some zero-length technique like look-around mechanism). I am thinking of something like

Pattern p = Pattern.compile(".(?=(.))");
Matcher m = p.matcher(yourString);
while(m.find()){
    System.out.println(m.group() + m.group(1));
}

You can also simply use substring(startIncluding, endExcluding)

String data = "Testing";
for (int i=0; i<data.length()-1; i++){
    System.out.println(data.substring(i,i+2));
}

or using charAt(index)

String data = "Testing";
for (int i = 0; i < data.length() - 1;) {
    System.out.append(data.charAt(i++)).println(data.charAt(i));
}
Pshemo
  • 118,400
  • 24
  • 176
  • 257
  • Thanks Pshemo for giving to me solution – Sanket990 Jul 03 '15 at 19:50
  • @Sanket990 I was hoping more for giving you explanation rather than solution. If some part of my answer is not clear feel free to ask about it. Otherwise my answer may be kind of pointless for you. – Pshemo Jul 03 '15 at 19:54
  • I am running your logic code and my criteria wise this answer is full filled i am already upvotted your answer .tnx – Sanket990 Jul 03 '15 at 20:05
  • It is not that I am concern about upvotes. It is more like I would rather know if you are able to easily understand my answer. If there is something unclear feel free to ask. – Pshemo Jul 03 '15 at 20:16
  • ya ya i got your logic and i am easily understand thanks. – Sanket990 Jul 03 '15 at 20:19
  • @Sanket990 I am glad to hear it :) BTW based on [list of your asked questions](http://stackoverflow.com/users/1237872/sanket990?tab=questions) it looks like you [accepted](http://meta.stackexchange.com/a/5235/186652) only [one answer](http://stackoverflow.com/a/23054213/1393766). Accepting answer is kind of "thank you" mechanism, and it also highlights for others answer which worked for asker which is also very useful. So when you are free you should take a look at your previous questions and check if there are answers which solved your problem (if yes accept best one for each question). – Pshemo Jul 03 '15 at 20:21
1

As @pshemo's answer said, a split consumes input, so you can't get a given character from the input in two parts of the split.

However, here a one-line solution that gets the job done:

Arrays.stream(s.split("")).reduce((a, b) -> {System.out.println(a + b); return b;});
Community
  • 1
  • 1
Bohemian
  • 389,931
  • 88
  • 552
  • 692