• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

question on java string manipulation

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the Sun/Oracle forums last week.
http://forums.sun.com/thread.jspa?threadID=5433217
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use *Code Tag*, while posting code
 
marcelo gobelli
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:No, but Be Forthright When Cross Posting To Other Sites.



trying to learn java. any help is appreciated.
 
What are you doing? You are supposed to be reading this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic