How can I split a string, but when a character repeats, don`t split?
Like:
String a = "aHHHbYY";
String[] b = a.split("");
But I don't want to split every letter just the ones that don`t repeat.
Output would be like:
["a", "HHH", "b", "YY"]
How can I split a string, but when a character repeats, don`t split?
Like:
String a = "aHHHbYY";
String[] b = a.split("");
But I don't want to split every letter just the ones that don`t repeat.
Output would be like:
["a", "HHH", "b", "YY"]
Your Problem can be solved easily with regular expressions. This could look like this:
String regex = "(?<=(.))(?!\\1)";
String a = "aHHHbYY";
String[] b = a.split(regex);
This gives the exact output you want. If you want a more detailed answer, take a look at this post:
You can use regular expressions as a solution. Here is a example for splitting and separating the chars and removing duplicates.
String input = "aHHHbYY";
String[] result = input.replaceAll("(.)\\1{1,}", "$1").split("");
will get you:
["a", "H", "b", "Y"]