Removes any leading or trailing [] on the string. - Java java.lang

Java examples for java.lang:String New Line

Description

Removes any leading or trailing [] on the string.

Demo Code

/**//from   ww  w  .ja  va  2 s  .com
 * 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 [] on the string.
     * 
     * @param stringToTrim
     *            The string to trim
     * @return String
     */
    public static String trimBrackets(String stringToTrim) {

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

        String trimmed = stringToTrim.trim();

        if (trimmed.startsWith("[")) //$NON-NLS-1$
        {
            trimmed = trimmed.substring(1);
        }

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

        return trimmed;
    }
}

Related Tutorials