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.
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
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
posted
0
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
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
posted
0
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.
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
posted
0
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
posted
0
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.)
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
posted
0
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: 1328
posted
0
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?
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
posted
0
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 ...