Friday, October 17, 2014

replacing newline with characters in java

I had a string named “remarks” with the following value in it

 

10/17/2014 07:14 AM test1

test1

test1

test1

10/17/2014 07:14 AM dfgdfg

 

I tried to replace all line feeds in this string with a particular character set in java and so I tried the following

 

String re = remarks1.replaceAll("\n","$#$”);

System.out.println(re);

 

This kept giving me illegal arguments exception.

 

So I tried

String re = remarks1.replaceAll("\\n","$#$");

System.out.println(re);

 

Now I felt weird. This is not normal behavior as most of my searches said this should work. Hence I felt a little irritated and I tried a different string instead of “$#$”. It was a stupid attempt though. But to my astonishment it worked fine

 

Curse compiler. Stupid same and its issue with “$” characters

 

Following worked at last

 

String re = remarks1.replaceAll("\\n","@#@");

System.out.println(re);

 

The output was

10/17/2014 07:14 AM test1@#@test1@#@test1@#@test1@#@10/17/2014 07:14 AM dfgdfg@#@

 

At ease 0.0  grrr