Removes any leading or trailing " or ' quotes on the string. - Java java.lang

Java examples for java.lang:String Quote

Description

Removes any leading or trailing " or ' quotes on the string.

Demo Code

/**/*from  www.  j a  v a2s  .c o m*/
 * Copyright (c) 2005-2006 Aptana, 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. If redistributing this code,
 * this entire header must remain intact.
 */
//package com.java2s;

public class Main {
    /**
     * Removes any leading or trailing " or ' quotes on the string. This is necessary as attribute values show as
     * strings from the lexer, and need to be surrounded by StringUtils.EMPTY
     * 
     * @param stringToTrim
     *            The string to trim
     * @return String
     */
    public static String trimStringQuotes(String stringToTrim) {

        if (stringToTrim == null) {
            return null;
        }

        String trimmed = stringToTrim.trim();

        if (trimmed.startsWith("\"") || trimmed.startsWith("'")) //$NON-NLS-1$ //$NON-NLS-2$
        {
            trimmed = trimmed.substring(1);
        }

        if (trimmed.endsWith("\"") || trimmed.endsWith("'")) //$NON-NLS-1$ //$NON-NLS-2$
        {
            trimmed = trimmed.substring(0, trimmed.length() - 1);
        }

        return trimmed;
    }
}

Related Tutorials