Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes trim() removes newline char at the front? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "trim() removes newline char at the front?" Watch "trim() removes newline char at the front?" New topic
Author

trim() removes newline char at the front?

srinadh penugonda
Greenhorn

Joined: Feb 13, 2007
Posts: 13
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?


A 1.0 [92%]<br />P 6.0 [preparing]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Can you think of an easy way to test this, then report back to us to let us know the results?


Steve
srinadh penugonda
Greenhorn

Joined: Feb 13, 2007
Posts: 13
That is also my dilemma. How to test this? How to put newline char in a test string???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38064
    
  22
Originally posted by srinadh penugonda:
That is also my dilemma. How to test this? How to put newline char in a test string???
Sounds more like a beginner's question.

Look in the Java Tutorials.
srinadh penugonda
Greenhorn

Joined: Feb 13, 2007
Posts: 13
Thanks
You guys have really been of great help!!!
Much appreciated.
- a java beginner
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

There is no such thing as a newline character - newline is a concept that is implemented differently based on the OS:

In dos/windows it is 0x0D0A (CARRIAGE RETURN, LINE FEED)

In Linux/Unix it is 0x0A (LINE FEED)

On a Mac it is 0x0D (CARRIAGE RETURN) [or at least that is what I recall before OS/X, not sure if it is still the case]

Therefore, yes, trim() will remove newlines because it removes the characters used to represent newlines on the various OSes.


JBoss In Action
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
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
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.
 
Consider Paul's rocket mass heater.
 
subject: trim() removes newline char at the front?
 
Similar Threads
int value of a char - is it unicode ?
removing the trailing space of a string only
Some tips to create these methods
return?
when and why we use FF and CR i,e.. \f and \r