Trimming Spaces from the Beginning or End of a String


String result = str.trim();



The TRim() method will remove both leading and trailing whitespace from a string and return the result. The original string will remain unchanged. If there is no leading or trailing whitespace to be removed, the original string is returned. Both spaces and tab characters will be removed.

This is very useful when comparing user input with existing data. A programmer often racks his brain for hours trying to figure out why what he enters is not the same as a stored string, only to find out that the difference is only a trailing space. Trimming data prior to comparison will eliminate this problem.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net