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.

8 Answers8

4

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

Vishnu Raj
  • 566
  • 1
  • 4
  • 14
3

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

Shirwa Mohamed
  • 191
  • 1
  • 9
  • 1
    Neither will work. First one will be a syntax error at compile time, second one will give `PatternSyntaxException` at runtime. – laalto Jan 08 '14 at 07:22
  • Oh I see the issue. Sorry about that. Check again. I have made edits. Use the second method. – Shirwa Mohamed Jan 08 '14 at 09:02
2

Try this:

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

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

Satyaki Mukherjee
  • 2,857
  • 1
  • 22
  • 26
2

use this code.

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

Amit kumar
  • 1,585
  • 2
  • 16
  • 27
2

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

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

Here is another thread about it.

String replace a Backslash

Community
  • 1
  • 1
ilkayaktas
  • 188
  • 1
  • 7
1

Try this..

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

Or

str.replaceAll(Pattern.quote("\\'"), "'")
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0
 public class XYZ {     
public static void main(String argss[])     {
        String p="123\' 5677fg\' ffd";
        System.out.println(p.replaceAll("/", ""));  }

}

try this

DropAndTrap
  • 1,538
  • 16
  • 24
0
String str = "Bebo\\'b";
str.replaceAll("\\","");
System.out.println(str);
learner
  • 3,092
  • 2
  • 21
  • 33