1

A string I retrieved from database contains \ which i need to remove.But as I put the replaceAll function it marks an error. Anyone got help please???

String I retrieved: Bebo\'b String required: Bebo'b 

I tried this function: str.replaceAll("\",""); marks an error where str is the retrieved string.

0

8 Answers 8

4

use function str.replaceAll("\\'","'");

Sign up to request clarification or add additional context in comments.

6 Comments

He already tried and mentioned in his question.
@Digital_Reality Sorry its been edited.
This is incorrect and will result in PatternSyntaxException
@crv str.replaceAll("\\",""); this is not work.
Use this code of piece String aa=ss.replaceAll("'\'","");
|
3

You should try the .replace("\", "") function. Otherwise you can try str.replaceAll("\'\", ""); using '\' as the escape sequence.

2 Comments

Neither will work. First one will be a syntax error at compile time, second one will give PatternSyntaxException at runtime.
Oh I see the issue. Sorry about that. Check again. I have made edits. Use the second method.
2

use this code.

String ss="Bebo\'b"; String aa=ss.replaceAll("'\'",""); tv.setText(aa);

Comments

2

This is a delimeter collision problem. You ha ve to do;

str.replaceAll("\\\\","");

Here is another thread about it.

String replace a Backslash

Comments

2

Try this:

String result=str.replaceAll("\'\'", ""); 

Though "\" treated as escape sequence character so it's single "\" is not workable .

2 Comments

This will result in PatternSyntaxException
@laalto I have modified and it's workable. I have tested it. Thanks. I have done little mistakes.
1

Try this..

str.replaceAll("\\",""); 

Or

str.replaceAll(Pattern.quote("\\'"), "'") 

Comments

0
 public class XYZ { public static void main(String argss[]) { String p="123\' 5677fg\' ffd"; System.out.println(p.replaceAll("/", "")); } } 

try this

2 Comments

he wants to replace backslash, not /
did u try this first try its working and getting desired output :)
0
String str = "Bebo\\'b"; str.replaceAll("\\",""); System.out.println(str); 

Comments