Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

question on java string manipulation  RSS feed

 
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.
 
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
 
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?
 
Sheriff
Posts: 21686
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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

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



trying to learn java. any help is appreciated.
 
Don't touch me. And dont' touch this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!