Java String Escape addEscapes(String str)

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

Description

Replaces unprintable characters by their espaced (or unicode escaped) equivalents in the given string.

License

Apache License

Parameter

Parameter Description
str the str

Return

the string

Declaration

public static final String addEscapes(String str) 

Method Source Code

//package com.java2s;
/*/*from   ww  w. j  av  a  2 s.c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 * Copyright (C) 2006-2010 Adele Team/LIG/Grenoble University, France
 */

public class Main {
    /**
     * Replaces unprintable characters by their espaced (or unicode escaped)
     * equivalents in the given string.
     * 
     * @param str
     *            the str
     * 
     * @return the string
     */
    public static final String addEscapes(String str) {
        StringBuilder sb = new StringBuilder();
        char ch;
        for (int i = 0; i < str.length(); i++) {
            switch (str.charAt(i)) {
            case 0:
                continue;
            case '\b':
                sb.append("\\b");
                continue;
            case '\t':
                sb.append("\\t");
                continue;
            case '\n':
                sb.append("\\n");
                continue;
            case '\f':
                sb.append("\\f");
                continue;
            case '\r':
                sb.append("\\r");
                continue;
            case '\"':
                sb.append("\\\"");
                continue;
            case '\'':
                sb.append("\\\'");
                continue;
            case '\\':
                sb.append("\\\\");
                continue;
            default:
                if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
                    String s = "0000" + Integer.toString(ch, 16);
                    sb.append("\\u" + s.substring(s.length() - 4, s.length()));
                } else {
                    sb.append(ch);
                }
                continue;
            }
        }
        return sb.toString();
    }
}

Related

  1. addEscapeCapacityToRegex(String inputRegex)
  2. addEscapeChar(String str)
  3. addEscapeCharactersForSpaces(String s)
  4. addEscapeCharsToBackSlash(String code)
  5. addEscapes(String s)
  6. addEscapeSequence(String inputStr)
  7. addEscapesToCode(String code)
  8. addEscChar(String str)
  9. escape(final @Nonnull String string)