aspose file tools*
The moose likes Java in General and the fly likes removing the trailing space of a string only Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "removing the trailing space of a string only" Watch "removing the trailing space of a string only" New topic
Author

removing the trailing space of a string only

kay lin
Ranch Hand

Joined: May 20, 2004
Posts: 132
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
saurabh sablok
Greenhorn

Joined: Mar 24, 2011
Posts: 4
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

Joined: Dec 09, 2010
Posts: 57
Regular expressions should do it for you
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38898
    
  23
Welcome to the Ranch

How on earth do you get trim() to remove a 0?
saurabh sablok
Greenhorn

Joined: Mar 24, 2011
Posts: 4

here is the code in which i m using trim() & its not working properly.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38898
    
  23
Why are you using the length as the second argument to String#substring()? Why don't you miss it out?
saurabh sablok
Greenhorn

Joined: Mar 24, 2011
Posts: 4
what should i use as end point in substring()?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4546
    
    5

http://download.oracle.com/javase/6/docs/api/java/lang/String.html#substring(int)


luck, db
There are no new questions, but there may be new answers.
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

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

Joined: Oct 13, 2005
Posts: 38898
    
  23
Welcome to the Ranch, Claudiu Chelemen Good idea, looking at Apache Commons; we tend to forget what useful stuff they have there.
 
Consider Paul's rocket mass heater.
 
subject: removing the trailing space of a string only