Remove the leading and trailing quotes from str in Java
Description
The following code shows how to remove the leading and trailing quotes from str.
Example
/*w ww . j a v a2 s.co m*/
public class Main {
public static void main(String[] argv) {
String line = "\"this is a test\"";
System.out.println(line);
System.out.println(stripLeadingAndTrailingQuotes(line));
}
static String stripLeadingAndTrailingQuotes(String str) {
if (str.startsWith("\"")) {
str = str.substring(1, str.length());
}
if (str.endsWith("\"")) {
str = str.substring(0, str.length() - 1);
}
return str;
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »