1

I'm having trouble replacing return characters in a JTextArea in windows 7. I have an input textArea that for data storage purposes I want to replace the "\r\n" with a unique string like "#!". Problem is, I can't seem to get it to replace it.

EX of issue: JTextArea exampleText = new JTextArea("Enter Text",10,3);
String oneLineOfText = exampleText.getText().replace("\r\n","#!");
System.out.println(oneLineOfText);

Input:
Text
Text everywhere

Output:
Text
Text everywhere

Desired Output: Text#!Text everywhere

I feel like I must be doing something really silly. This works perfectly fine in ubuntu when I use "\n" instead of "\r\n".

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
  • 2
    `\r\n` I believe is a Windows line ending. You could use a regular expression to try and trap different terminators, for example `text.replaceAll("\r\n|\n", "#!")`...note - not tested ;) – MadProgrammer Feb 17 '14 at 05:17
  • Thanks for the quick response, MadProgrammer. This may work except I get this error. "java.lang.string.replaceall(unknown source)". I tried "\\r\\n|\\n" which also yielded the same error. – pancakes4ever Feb 17 '14 at 06:12
  • `replaceAll` - Java is case sensitive, I'm assuming your using Java 1.4+ – MadProgrammer Feb 17 '14 at 06:26
  • Whoops, it was actually replaceAll in the code and yes, I'm on 1.7 – pancakes4ever Feb 17 '14 at 13:40

3 Answers3

2

As I understand it, \r\n is a Windows line terminator.

Instead of looking for just a single line terminator, you could look for multiples and replace them.

For this you could use a regular expressiong and String#replaceAll, for example...

//String text = "This is\r\na test\r\nfor some text";
String text = "This is\na test\r\nfor some text";
System.out.println(text);
text = text.replaceAll("\r\n|\n", "#!");
System.out.println(text);

Which outputs...

This is
a test
for some text
This is#!a test#!for some text

You should also note, that not all text editors/text files have the \r\n line terminator, but Java does a pretty job of dealing with this...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Don't hard-code the newline character, use the system newline character:

System.getProperty("line.separator");

How do I get a platform-dependent new line character?

Community
  • 1
  • 1
Charlie
  • 8,530
  • 2
  • 55
  • 53
  • This was actually the first thing I tried. In hindsight, I have an idea of why it may not have worked. Does it grab this when the code is compiled or when the program in executed? – pancakes4ever Feb 17 '14 at 13:38
  • While it's certainly a good suggestion, what happens when you're dealing with a file from a different OS then the one the JVM is running on, the property becomes irrelevant - just saying ;) – MadProgrammer Feb 17 '14 at 19:34
  • I figured the replacement was happening on submission of the text box, which using the system line separator would be perfect for. – Charlie Feb 17 '14 at 23:51
  • The property is evaluated at runtime, not compile time. – Charlie Feb 17 '14 at 23:51
0

open a text file, and save the text of JTextArea in it. Now read the text from the saved text file charater by character by checking the following line,

string variable s="";
if(ch=='\n')
   add the #! to the string variable;

finally place this string variable to the output section.

Balayesu Chilakalapudi
  • 1,386
  • 3
  • 19
  • 43