3

Is there any simple way to split sorted string that has only letters(no spaces).

For example if I have this string:

  string str ="aaasssdeettyy";

I need to split this string to sub strings:

string res = "aaa";
string res = "sss";
string res = "d";
string res = "ee";
string res = "tt";
string res = "yy";

Is there any way to implement it using split() command and regex expression?

Michael
  • 13,950
  • 57
  • 145
  • 288

1 Answers1

0

Try this one: \1 matches the same text as matched by the first group (.)

Pattern compile = Pattern.compile("(.)\\1+");
Matcher matcher = compile.matcher("aaabbbcdddee");
while (matcher.find())
    System.out.println(matcher.group());
Jérôme
  • 1,254
  • 2
  • 20
  • 25