Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *   @(#) $Id: CollectionUtil.java 60 2005-08-18 09:06:14Z trustin $
 *
 *   Copyright 2004 Trustin Lee
 *
 *   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.
 *
 */

public class Main {
    public static String escape(String value) {
        boolean wrapWithQuote = false;
        // +8 is a spare padding for quote expansion
        int len = value.length();
        StringBuffer buf = new StringBuffer(len + 8);
        int i;
        char c;

        if (len == 0)
            return "\"\"";
        if (Character.isWhitespace(value.charAt(0)))
            wrapWithQuote = true;
        else if (Character.isWhitespace(value.charAt(len - 1)))
            wrapWithQuote = true;

        for (i = 0; i < len; i++) {
            c = value.charAt(i);

            switch (c) {
            case ',':
            case '=':
                wrapWithQuote = true;
                buf.append(c);
                break;
            case '"':
                wrapWithQuote = true;
                buf.append("\"\"");
                break;
            default:
                buf.append(c);
            }
        }

        if (wrapWithQuote) {
            return "\"" + buf.toString() + '"';
        } else {
            return buf.toString();
        }
    }
}