Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replacing "enter" in Strings

 
Rachel Swailes
Ranch Hand
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All characters are just integers in Java. To see their value use:
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

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

Rachel
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic