Wednesday, June 22, 2011

String utility : unquoting the string

This is another simple Java string utility method, which can be used to unquote a string value. This method takes care of single and double quotes both along with handling the null string. It returns the string as it is when the string is not quoted.

public static String unquote(String s) {
       if (s != null    && ((s.startsWith("\"") && s.endsWith("\""))
                                || (s.startsWith("'") && s.endsWith("'")))) {

                s = s.substring(1, s.length() - 1);
       }
     return s;
}

No comments:

Post a Comment

Chitika