Hi java experts, If I use trim() on a string that might contain newline char, will it be removed? From the documentation, it says trim() removes leading and trailing whitespaces. At the same time, I see this: A character is a Java whitespace character if and only if it satisfies one of the following criteria: It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F'). It is '\u0009', HORIZONTAL TABULATION. It is '\u000A', LINE FEED. It is '\u000B', VERTICAL TABULATION. It is '\u000C', FORM FEED. It is '\u000D', CARRIAGE RETURN. It is '\u001C', FILE SEPARATOR. It is '\u001D', GROUP SEPARATOR. It is '\u001E', RECORD SEPARATOR. It is '\u001F', UNIT SEPARATOR.
So, if I assume trim() removes any of the above characters, is my assumption right?
The definition of whitespace characters you quoted comes from from the Character API docs; it has no bearing on the trim() method. The trim() doc tells exactly what it removes: characters whose ASCII value is less than or equal to 32 (or 0x20, the space character). That includes linefeeds and carriage returns. [ August 21, 2008: Message edited by: Alan Moore ]
Barclay Dunn
Greenhorn
Joined: Nov 16, 2011
Posts: 3
posted
0
Here is a sample test class that you might run to see how trim() works on some strings:
Uncomment the testString on which you want to see what the effect of trim() is, and run the code. If you use an IDE, you can probably run the code right in the IDE.