• Post Reply Bookmark Topic Watch Topic
  • New Topic

question on java string manipulation  RSS feed

 
marcelo gobelli
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the Sun/Oracle forums last week.
http://forums.sun.com/thread.jspa?threadID=5433217
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use *Code Tag*, while posting code
 
marcelo gobelli
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:On the Sun/Oracle forums last week.
http://forums.sun.com/thread.jspa?threadID=5433217


yes, and if you look at "in reply to #8" I am still waiting for an answer. Is it incorrect to post the same issue to both forums?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, but Be Forthright When Cross Posting To Other Sites.
 
marcelo gobelli
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


trying to learn java. any help is appreciated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!