Java String Unquote unquote(String s, char c)

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

Description

Useful for unquoting strings, since StringTokenizer won't do it for us.

License

Open Source License

Parameter

Parameter Description
s a parameter
c a parameter

Declaration


public static final String unquote(String s, char c) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* w  w  w.j  a v a 2  s. c  o m*/
     * Useful for unquoting strings, since StringTokenizer won't do it for us.
     * Returns null upon any failure.
     *
     * @param s
     * @param c
     * @return
     */

    public static final String unquote(String s, char c) {

        int firstq = s.indexOf(c);
        int lastq = s.lastIndexOf(c);
        // Indexes valid?
        if (isQuoted(s, c))
            return s.substring(firstq + 1, lastq);

        return null;
    }

    /**
     * Finds index of first element of array matching key. Useful whenever an
     * "indexOf" property is required or you encounter the C-ism [pointer-
     * array_base] used to find array indices in O(1) time. However, since this
     * is just a dumb unsorted search, running time is O(n), so use this method
     * only sparingly and in scenarios where it won't occur very frequently
     * -once per level is probably OK-, but watch out for nested loops, and
     * cache the result whenever possible. Consider adding an index or ID type
     * of field to the searched type if you require to use this property too
     * often.
     *
     * @param array
     * @param key
     * @return
     */

    public static int indexOf(Object[] array, Object key) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] == key) {
                return i;
            }
        }

        return -1;
    }

    public static final boolean isQuoted(String s, char c) {

        int q1 = s.indexOf(c);
        int q2 = s.lastIndexOf(c);
        char c1, c2;

        // Indexes valid?
        if (q1 != -1 && q2 != -1) {
            if (q1 < q2) {
                c1 = s.charAt(q1);
                c2 = s.charAt(q2);
                return (c1 == c2);
            }
        }

        return false;
    }
}

Related

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