This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem when doing replace

 
Ganesan Ramakrishnan
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Here is the string i typed into the text area

hai
all
how
are

i want to print this 4 line into one line .
so output like this "hai all how are" in one line
i.e i want to replace the newline character with space

i have done like this store this string into one string
then
str.replaceAll("\\n","");

but its not working

Regards
Ganesan
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Are you sure you are giving a "space" in your replace code , like this

 
Ganesan Ramakrishnan
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes is there .

But the problem is i am going to writing this string into .csv file is again showing the with newline.

Thanks
Ganesan
 
Sachin Adat
Ranch Hand
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you should use 2 \\ characters. I think you need only 1.
Try to replace \n and also assign the replaced value to your string.

with
 
Sagar Rohankar
Ranch Hand
Posts: 2904
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh sory for that doubt !!

The problem is String#replaceAll() returns a new String object!!

So better you catch it , like



Try this ,
 
Ganesan Ramakrishnan
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks for all.

I tried before itself like String str = str.replaceAll("\n" , "");
but its not working.

But str = str.replaceAll("\r" , " ");

its working fine.

once again thanks for all

Regards
Ganesan
 
Bill Shirley
Ranch Hand
Posts: 457
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look up the API docs for

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic