• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Replacing "enter" in Strings

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic