This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem when doing replace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem when doing replace" Watch "Problem when doing replace" New topic
Author

Problem when doing replace

Ganesan Ramakrishnan
Ranch Hand

Joined: Mar 18, 2008
Posts: 88
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

Joined: Feb 19, 2008
Posts: 2902
    
    1



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



[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ganesan Ramakrishnan
Ranch Hand

Joined: Mar 18, 2008
Posts: 88
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

Joined: Sep 03, 2007
Posts: 213
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


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Mar 18, 2008
Posts: 88
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

Joined: Nov 08, 2007
Posts: 457
You might want to look up the API docs for



Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem when doing replace
 
Similar Threads
unit testing the java method
Removing or replacing a enter key chararcter from a string??
Object Create
Connect to diff pages
read file replace string