I have this string:
String s = "########## \r\n# #########\r\n";
I want to run length encode the results. I am successful at encoding it:
10#6-|#5-9#|. I am using a delimeter here: "|". I am also using the replace ('-') for whitespace
I need help with this: how to suppress the trailing whitespace if there is only whitespace before the delimeter. I want my output to be:
10#|#5-9#|
I have a method that does the encoding and do not want to do it with a regex in the parser. This is the method:
public String encode(String input) {
StringBuffer dest = new StringBuffer();
for (int i = 0; i < input.length(); i++) {
int runLength = 1;
while( i+1 < input.length() && input.charAt(i) == input.charAt(i+1)
) {
runLength++;
i++;
}
if (runLength == 1) {
dest.append(input.charAt(i));
}
if (runLength == 2){
//dest.append(runLength);
dest.append(input.charAt(i));
dest.append(input.charAt(i));
}
if ((runLength > 2) {
dest.append(runLength);
dest.append(input.charAt(i));
}
}
return dest.toString();
}
Thanks for the help.