Given: String str1= "abc\"def";
String str2= "\"1234\"";
Required: String str1 = "abcdef";
String str2 = "1234";
You have several implementation options that mainly fall into two
catagories: Code a loop that scans throught the characters of the string
and build up a new string without the quotes, or use regular
expressions. The loop is good if you want to control resources (CPU and
RAM) spend solving the problem, and the regex is good if you just want
to write it simple.
If you use Java 1.5, the regex can be done like:
String str1 = " .... ";
String str1WithoutQuotes = str1.replaceAll("\"","");
In Java 1.4 the same effect can be achieved by
String str1WithoutQuotes =
java.util.regex.Pattern.compile("\"").matcher(str1).replaceAll("");
If you have plenty of those I recommend you put it into a convenience
method somewhere like
private static final java.util.regex.Pattern QUOTE_PATTERN =
Pattern.compile("\"");
public static String removeQuotes(String s) {
return QUOTE_PATTERN.matcher(s).replaceAll("");
}
Regards,