Java String Escape addEscape(String a_unescaped)

Here you can find the source of addEscape(String a_unescaped)

Description

Add escape characters (\) before each of the characters to escape and each of the escape characters themselves.

License

Open Source License

Parameter

Parameter Description
a_unescaped a parameter

Return

the escaped text string

Declaration

public static String addEscape(String a_unescaped) 

Method Source Code

//package com.java2s;
/*// ww  w. j  a va2 s . c  o m
 * @(#) StringUtils.java Jul 20, 2005
 * Copyright 2005 Frequency Marketing, Inc. All rights reserved.
 * Frequency Marketing, Inc. PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class Main {
    /**
     * The string escape character
     */
    public static final char ESCAPE_CHAR = '\\';

    /**
     * Add escape characters (\) before each of the characters to escape and
     * each of the escape characters themselves.
     * @param a_unescaped
     * @return the escaped text string
     */
    public static String addEscape(String a_unescaped) {
        String escaped = null;

        if (null != a_unescaped) {
            StringBuffer text = new StringBuffer();

            char charToEscape = ',';

            char character;
            int length = a_unescaped.length();
            for (int index = 0; index < length; index++) {
                character = a_unescaped.charAt(index);

                if (character == charToEscape || character == ESCAPE_CHAR) {
                    text.append(ESCAPE_CHAR);
                }

                text.append(character);
            }

            escaped = text.toString();
        }

        return escaped;
    }
}

Related

  1. addEscapeCapacityToRegex(String inputRegex)
  2. addEscapeChar(String str)
  3. addEscapeCharactersForSpaces(String s)
  4. addEscapeCharsToBackSlash(String code)