Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

removing the trailing space of a string only

 
kay lin
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
the trim() removes both the trailing and leading space, however if i only want to remove the trailing space of a string, i guess i will have to write my own routine, correct?

here is the code that removes the trailing space only



bascially i took the string in reverse and exit the loop as soon as it sees the first non white space character. Is there any easier way to do this? i thought mine was still a bit complicated....

Many thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that's about it. You might want a version that lets you pass in the character to remove so you could trim trailing zeros or asterisks or whatever. trim() removes all whitespace, not just blanks. Don't know if you want to do that or not.
 
saurabh sablok
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m using trim to remove space but i dont want to remove the trailing zero from string.
it is changing 10 to 1.what can i use instead of trim().
 
Kowshik Nandagudi
Ranch Hand
Posts: 57
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regular expressions should do it for you
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

How on earth do you get trim() to remove a 0?
 
saurabh sablok
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

here is the code in which i m using trim() & its not working properly.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using the length as the second argument to String#substring()? Why don't you miss it out?
 
saurabh sablok
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what should i use as end point in substring()?
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#substring(int)
 
Claudiu Chelemen
Ranch Hand
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

To remove only the trailing characters from the String, you could use stripEnd method from apache commons StringUtils.
Check the link for details.

http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html#stripEnd(java.lang.String, java.lang.String)

Cheers!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Claudiu Chelemen Good idea, looking at Apache Commons; we tend to forget what useful stuff they have there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic