Java String Unquote unquote(String s)

Here you can find the source of unquote(String s)

Description

unquote

License

Apache License

Declaration

public static String unquote(String s) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String unquote(String s) {
        if (s == null) {
            return "";
        }/* w w w.j  av  a2 s.  c o  m*/
        if (startsWith(s, "'")) {
            return unwrap(s, "'", "'");
        } else if (startsWith(s, "\"")) {
            return unwrap(s, "\"", "\"");
        }
        return s.trim();
    }

    public static boolean startsWith(String s, String prefix) {
        if (s == null) {
            return false;
        }

        for (int i = 0; i < s.length(); i++) {
            if (Character.isWhitespace(s.charAt(i))) {
                continue;
            } else {
                return s.regionMatches(i, prefix, 0, prefix.length());
            }
        }

        return false;
    }

    public static String unwrap(String s, String prefix, String suffix) {
        int start = 0, end = s.length() - 1;

        while (start < s.length()) {
            if (!Character.isWhitespace(s.charAt(start))) {
                break;
            }
            ++start;
        }

        while (end >= 0) {
            if (!Character.isWhitespace(s.charAt(end))) {
                break;
            }
            --end;
        }

        return s.substring(start + prefix.length(), end - suffix.length() + 1);
    }
}

Related

  1. unquote(String s)
  2. unquote(String s)
  3. unquote(String s)
  4. unquote(String s)
  5. unquote(String s)
  6. unquote(String s)
  7. unquote(String s)
  8. unquote(String s)
  9. unquote(String s)