aspose file tools*
The moose likes Java in General and the fly likes Trimming trailing whitespace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Trimming trailing whitespace" Watch "Trimming trailing whitespace" New topic
Author

Trimming trailing whitespace

Bob Reardon
Ranch Hand

Joined: Jun 01, 2000
Posts: 160
Is there a method in Java that only removes trailing whitespace? I need to preserve the format of the data I am diaplaying, but the trailing spaces make the lines too long to be properly displayed on the web page.

I am going through some of the regular expressions sites to see if I can find something that might help. Please let me know if you have solved this problem in the past.

Thanks,
-Bob
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I don't know how efficient this is (creating a new String object every time it chops off a space), but this works...



(It would be nice to use a regex to find the lastIndexOf anything that's not a space, and then use that value to create a new substring in one call, but I don't see any lastIndexOf methods taking a regex argument.)
[ October 15, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Can you just use the API method String.trim() (which removes both leading and trailing whitespace?)


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Oh, duh... This is much better. Find the last character that's not a space, then return a substring based on that index.


(Yeah, I'm assuming that for some reason you want to keep any leading whitespace, so trim() won't work. Right?)
[ October 15, 2004: Message edited by: marc weber ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Marc, I made something very similar that will remove leading and/or trailing instances of any character. So this would be:

trimmed = StringUtil.strip(input, "T", " ");

I borrowed the concept and syntax from the REXX language, which allows any abbreviation of "TRAILING" in the second argument, eg "TR"


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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Stan James:
trimmed = StringUtil.strip(input, "T", " ");

...allows any abbreviation of "TRAILING" in the second argument...

Interesting. Is the third argument for "LEADING"? (Maybe I should add this to my library. I really need to get better about saving code snippets.)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Here's an improved method that can also handle all blanks (" "), empty Strings (""), and null values...


[ October 16, 2004: Message edited by: marc weber ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
This is probably a good exercise to just learn how to do it. However, in production level code, I suggest that you just use String.trim() from the standard API instead.

Layne


Java API Documentation
The Java Tutorial
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Layne Lund:
This is probably a good exercise to just learn how to do it. However, in production level code, I suggest that you just use String.trim() from the standard API instead.

Layne

Yes, but trim() removes both leading and trailing whitespace. What if you wanted to keep one or the other? (I don't know why, but that was the original problem.)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Just for grins, the REXX syntax second argument is LEADING, TRAILING or BOTH. The interpreter is very friendly, accepting upper or lower case, any truncation like LEAD or TRAIL or BO. L T and B are far and way the most commonly seen. It's easy to support this kind of thing:

if ("LEADING".startsWith(arg.toUpperCase()) ...

I agree with Layne ... for readability and efficiency use trim() where it meets the requirements. If you installed source with your JDK you might read trim() to see how the Sun guys did it. You could do worse than copying them!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by marc weber:

Yes, but trim() removes both leading and trailing whitespace. What if you wanted to keep one or the other? (I don't know why, but that was the original problem.)

Good point.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Actually, when I first considered this problem (eliminating only the trailing whitespace), my initial idea was to count the number of any leading spaces, then use trim(), then put the leading spaces back.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
If you installed source with your JDK you might read trim() to see how the Sun guys did it. You could do worse than copying them!


Well, I don't know about trim(), but in many cases you actually could do much better. Take a look at GregorianCalendar...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
marc weber:

[I]Here's an improved method that can also handle all blanks (" "), empty Strings (""), and null values...

[/I]

Any particular reason not to use the Character.isWhitespace() function?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Warren Dew:
...Any particular reason not to use the Character.isWhitespace() function?

I had forgotten that the Character wrapper has so many unique methods (compared to the numeric wrappers). Thanks for pointing this out!
Bob Reardon
Ranch Hand

Joined: Jun 01, 2000
Posts: 160
Thanks for all of your help in solving my problem. I ran in to this problem while taking billing information that was generated on our mainframe and displaying it on the web. The lines generted for the billing report were too long and needed to have their trailing spaces trimmed. But, I needed to keep the leading spaces to maintaining the formatting. (See example)

example header line:
<pre>
Date Description Total Minimum
Due
...

</pre>

Thanks again for all of you help.
-Bob
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

What's about:


http://home.arcor.de/hirnstrom/bewerbung
Bob Reardon
Ranch Hand

Joined: Jun 01, 2000
Posts: 160
Originally posted by Stefan Wagner:
What's about:


That Works!! Thanks Stefan!!
Collin VanDyck
Greenhorn

Joined: Mar 06, 2004
Posts: 4
Originally posted by Stefan Wagner:
What's about:


Also, you could use:



Where \s (escaped properly in a String as \\s) is the regex shorthand for whitespace, which will include such characters as tabs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trimming trailing whitespace