Encloses the incoming string inside double quotes, if it isn't already quoted. - Android java.lang

Android examples for java.lang:String Format

Description

Encloses the incoming string inside double quotes, if it isn't already quoted.

Demo Code

/*//from w  w w. ja v a2 s  . com
 * Copyright (C) 2010 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

import android.text.TextUtils;

public class Main {
    /**
     * Encloses the incoming string inside double quotes, if it isn't already quoted.
     * @param string: the input string
     * @return a quoted string, of the form "input".  If the input string is null, it returns null
     * as well.
     */
    static String convertToQuotedString(String string) {
        if (string == null) {
            return null;
        }
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        int lastPos = string.length() - 1;
        if (lastPos < 0
                || (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
            return string;
        }
        return '\"' + string + '\"';
    }
}

Related Tutorials