Java String Unquote unquoteS(String s)

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

Description

Take a string delimited with single quotes and remove the delimiters.

License

Open Source License

Parameter

Parameter Description
s The input string.

Return

The unquoted string; if the input is null or not delimited by single quotes return unchanged.

Declaration

public static String unquoteS(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Firestar Software, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://ww w . jav a 2  s .  c  o  m
 *     Firestar Software, Inc. - initial API and implementation
 *
 * Author:
 *     Gabriel Oancea
 *
 *******************************************************************************/

public class Main {
    /**
     * Take a string delimited with single quotes and remove the delimiters. Also if there are any escaped single quotes
     * in the string un-escape them. For example:
     * <p>
     * <code>'Arthur''s sword'</code>
     * <p>
     * becomes:
     * <p>
     * <code>Arthur's sword</code>
     * 
     * @param s The input string.
     * @return The unquoted string; if the input is null or not delimited by single quotes return unchanged.
     */
    public static String unquoteS(String s) {
        return unquoteImpl(s, '\'');
    }

    private static String unquoteImpl(String s, char delim) {
        if (s == null || s.length() < 2)
            return s;
        if (!(s.charAt(0) == delim && s.charAt(s.length() - 1) == delim))
            return s;
        if (s.length() == 2)
            return "";
        s = s.substring(1, s.length() - 1);
        StringBuffer sb = new StringBuffer(s.length());
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == delim) {
                if (i + 1 < s.length()) {
                    char d = s.charAt(i + 1);
                    if (d == delim)
                        i++;
                }
            }
            sb.append(c);
        }
        return sb.toString();
    }
}

Related

  1. unQuoteIdentifier(String identifier, boolean useAnsiQuotedIdentifiers)
  2. unQuoteIdentifier(String identifier, String quoteChar)
  3. unQuoteIdentifier(String identifier, String quoteChar)
  4. unquoteImpl(String s, char delim)
  5. unquotePath(String path)
  6. unquoteString(final String input)
  7. unquoteString(String s)
  8. unquoteString(String s)
  9. unquoteString(String s)