Question:
I need to process some strings in Java. I know there is a trim()
method, but I need a Left Trim and a Right Trim .
How do I do this?
For now I'm looping through the string and stripping all whitespace from the beginning (until it hits a character) or from the end ( looping from the end to the beginning of the string ).
Answer:
You can use regex:
Right Trim:
String texto_filtrado = original.replaceAll("\\s+$", "");
Left Trim:
String texto_filtrado = original.replaceAll("^\\s+", "");
Source: http://www.xinotes.org/notes/note/1418/