I have a string like below:
String str = "77755529";
I want to split this string if different number occur i.e result should be like below after splitting :
str1 = "777";
str2 = "555";
str3 = "2";
str4 = "9";
I tried it with split but could not make it.
Try this:
String str = "77755529";
String[] res = str.split("(?<=(.))(?!\\1)");
IDEONE SAMPLE
You may do matching.
List<String> lst = new ArrayList<String>();
Matcher m = Pattern.compile("(\\d)\\1+|\\d+").matcher(s);
while(m.find()) {
lst.add(m.group());
}
System.out.println(lst);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With