• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trimming trailing whitespace

 
Bob Reardon
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you just use the API method String.trim() (which removes both leading and trailing whitespace?)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's about:
 
Bob Reardon
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
What's about:


That Works!! Thanks Stefan!!
 
Collin VanDyck
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic