RaagaSudha
RaagaSudha

Reputation: 397

Replacing the string in android?

In my application I am displaying text from database in a textview. The text contains '\r\n'. So I replaced '\r\n' with empty i.e with ' '.

My code:

String myString = listItem.gettextdata().replace("\r\n", " "); 

But still the text is displaying with \r\n....where I went wrong? Please help me regarding this....

Thanks in advance

Upvotes: 0

Views: 1234

Answers (6)

user370305
user370305

Reputation: 109237

Simply:

    String str;
    str = "hello\r\njava\r\nbook";
    str = str.replaceAll("(\\r|\\n)", " ");
    System.out.println(str);

Or

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

Upvotes: 1

waqaslam
waqaslam

Reputation: 68177

try this:

String myString = listItem.gettextdata().replaceAll("[\r\n]", "");  

Upvotes: 0

Jon
Jon

Reputation: 3194

What if you try a .toString() after gettestdate():

String myString = listItem.gettextdata().toString().replace("\r\n", " ");  

Upvotes: 1

AndroidDev
AndroidDev

Reputation: 2647

Use code below :

YourString.replaceAll("\r\n", ""); 

Upvotes: 1

colegu
colegu

Reputation: 370

You should use

listItem.getText().toString().replace...

Upvotes: 0

Philip Sheard
Philip Sheard

Reputation: 5825

Is this a case where you have to escape the backslash character, i.e.

String myString = listItem.gettextdata().replace("\\\\r\\\\n", " "); 

Upvotes: 5

Related Questions