Java String Quote quoteAndClean(String str)

Here you can find the source of quoteAndClean(String str)

Description

quote And Clean

License

Apache License

Declaration

private static String quoteAndClean(String str) 

Method Source Code

//package com.java2s;
/*/*from w w w . j  ava  2 s. co m*/
 * Copyright 2012 LinkedIn Corp.
 *
 * 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 {
    private static String quoteAndClean(String str) {
        if (str == null || str.isEmpty()) {
            return "\"\"";
        }

        StringBuffer buffer = new StringBuffer(str.length());
        buffer.append('"');
        for (int i = 0; i < str.length(); ++i) {
            char ch = str.charAt(i);

            switch (ch) {
            case '\b':
                buffer.append("\\b");
                break;
            case '\t':
                buffer.append("\\t");
                break;
            case '\n':
                buffer.append("\\n");
                break;
            case '\f':
                buffer.append("\\f");
                break;
            case '\r':
                buffer.append("\\r");
                break;
            case '"':
            case '\\':
            case '/':
                buffer.append('\\');
                buffer.append(ch);
                break;
            default:
                if (isCharSpecialUnicode(ch)) {
                    buffer.append("\\u");
                    String hexCode = Integer.toHexString(ch);
                    int lengthHexCode = hexCode.length();
                    if (lengthHexCode < 4) {
                        buffer.append("0000".substring(0, 4 - lengthHexCode));
                    }
                    buffer.append(hexCode);
                } else {
                    buffer.append(ch);
                }
            }
        }
        buffer.append('"');
        return buffer.toString();
    }

    private static boolean isCharSpecialUnicode(char ch) {
        if (ch < ' ') {
            return true;
        } else if (ch >= '\u0080' && ch < '\u00a0') {
            return true;
        } else if (ch >= '\u2000' && ch < '\u2100') {
            return true;
        }

        return false;
    }
}

Related

  1. quote(String value)
  2. quote(String value)
  3. quote(StringBuilder buf, String str)
  4. quote(StringBuilder sb, String str)
  5. quote4CMDLine(String str)
  6. quoteAndEscape(String str)
  7. quoteAndEscapeFilename(String filename)
  8. quoteAndEscapeValue(String serviceQName)
  9. quoteArg(final String arg)