1

friends,

i am facing an issue

when i display someone's post in android listview it shows me

someone\'s post

i want to remove \ from string and wrote following code which give me outofmemory error

if(val.contains("\\"))
        {
        val=val.replace("", "\\");
        }

any one guide me whats the soultion?

2 Answers 2

2

Doesn't replace work the other way round?

val = val.replace("\\", "");
Sign up to request clarification or add additional context in comments.

Comments

1

Here's an excerpt from the documentation:

public String replace(CharSequence target, CharSequence replacement):
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

So the error in this particular case is that you've swapped the arguments around.

System.out.println(  "a\\b"                    ); // "a\b"
System.out.println(  "a\\b".replace("", "\\")  ); // "\a\\\b\"
System.out.println(  "a\\b".replace("\\", "")  ); // "ab"

Note that you don't really need to do an if/contains check: if target is not found in your string, then no replacement will be made.

System.out.println("a+b".replace("\\", "")); // "a+b"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.