-1

Hi I am trying to retain backslash in a string but backslash is getting printed and when it is inserted in redshift database it is inserted as two double quote.Can any one help here please!

str="I\"THIS IS TEST[1]\""
String resStr = str.replaceAll("[^\\x00-\\x7F]|[\u0001]|[\uFF3C]", "");
System.out.println("resStr----"+resStr);

prints as : I"THIS IS TEST[1]" and when it is inserted in redshift db , is inserted as I""THIS IS TEST[1]"" (i.e. with two doubt quote , not sure why)

How to retain the backslash \ in the String str. Please need help here.

sat1219
  • 25
  • 1
  • 5
  • Hint: read the first answer of the linked question. It tells you how to include a backslash in a string or char literal. In a literal, a backslash followed by a double-quote represents a double-quote character. – Stephen C Mar 15 '19 at 02:08

1 Answers1

1

You seem to confused regarding how to represent a literal backslash versus a literal double quote in a Java string. A literal backslash is represented by two backslahses:

String input = "A\\B\\C";
System.out.println(input);

A\B\C

On the other hand, a literal double quote is represented by escaping a double quote with a single (not double) backslash:

String input = "\"ABC\"";
System.out.println(input);

"ABC"
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks Tim! , Sorry for confusions, Yeah basically the data which comes to the java application is like this: I\"THIS IS TEST[1]\" , so if we want to retain both \ and " , then we need to update the data as I\\\"THIS IS TEST[1]\\\" for the data : I\"THIS IS TEST[1]\" - to be inserted into the Redshift. – sat1219 Mar 18 '19 at 01:10
  • But the above thing has to be done only if we have \" So i am searching how to identify the \" in a given string, tried with regEx , but no luck. Can you please suggest here how to check if the string has \" so that if a match is found then i will replaceAll ("\"" , "\\\""); – sat1219 Mar 18 '19 at 01:12
  • At this point you may wish to open a new question. – Tim Biegeleisen Mar 18 '19 at 01:21