wood burning stoves*
The moose likes Java in General and the fly likes Replacing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Replacing "enter" in Strings" Watch "Replacing "enter" in Strings" New topic
Author

Replacing "enter" in Strings

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
I have a string that I get from a JTextArea. In the text area, the message looks like this

Hi there
Hope you have a good day
Cheers, Rachel

Now I need to replace the end of lines with the '\n' character.
I have tried to determine how this character is stored in the string by looking on the net and by trying this
System.out.println(myString.lastIndexOf('thecharacter'));
Where the character was
\n
\r
\f
and '\u000D' (but this one doesnt compile)

Does anyone know how to find this character?

Thanks
Rachel
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
I think it's already '\n'
Looking at the code in PlainDocument which is created if you don't supply a document, when your string is inserted it may be filtered, and if so the '\n' is replaced by ' '.

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi Ta Ri Ki Sun

I already tried searching the string for '\n' but it does not appear. When I write my string to the screen it appears like this

Hi
How are YOu
Rachel

And when I write the string to the text file it does write it in one line, but there are little squares where the returns are. How do I find out what those little squares represent?

Thanks
Rachel
Fletcher Estes
Ranch Hand

Joined: Jul 01, 2004
Posts: 108
All characters are just integers in Java. To see their value use:
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
Originally posted by Rachel Swailes:
Hi Ta Ri Ki Sun

I already tried searching the string for '\n' but it does not appear. When I write my string to the screen it appears like this

Hi
How are YOu
Rachel

And when I write the string to the text file it does write it in one line, but there are little squares where the returns are. How do I find out what those little squares represent?

Thanks
Rachel


Hi Rachel
I wrote a simple class that tests this, and here's my source and results.



116 t
101 e
115 s
116 t
49 1
10

116 t
101 e
115 s
116 t
50 2
10

116 t
101 e
115 s
116 t
51 3
test1
test2
test3


so you can check for an int == 10, which on the ascii table says
Dec 10
Hex A
Oct 012
Char LF
(NL line feed, new line)

Dunno if that's the right way to do it or not, maybe someone more knowledgable on the subject will clear it up
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

Comparing each character's int value for 10 seems to have done the trick. Thanks for all your help!!

Rachel
 
wood burning stoves
 
subject: Replacing "enter" in Strings
 
Similar Threads
combining characters - \\r\\n to \r\n
Carriage Returns in Request Dispatcher attribute.
Saving esacpe characters to file
String replace
Pseudo code