Java String Escape addEscapeChar(String str)

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

Description

Description:Convert string which contains '%','_' to '\%' and '\_'

License

Open Source License

Parameter

Parameter Description
input string

Return

output string

Declaration

public static String addEscapeChar(String str) 

Method Source Code

//package com.java2s;
/*/*  w  ww .  j  a  v  a 2  s .  c om*/
 * $RCSfile: LogStringUtil,v $$
 * $Revision: 1.0  $
 * $Date: 2010-12-09  $
 *
 * Copyright (C) 2011 GyTech, Inc. All rights reserved.
 *
 * This software is the proprietary information of GyTech, Inc.
 * Use is subject to license terms.
 */

public class Main {
    /**
     * <p>Description:Convert string which contains '%','_' to '\%' and '\_'</p>
     * @param input string
     * @return output string
     */
    public static String addEscapeChar(String str) {
        String output = str.replaceAll("%", "\\\\" + "%");
        output = output.replaceAll("_", "\\\\" + "_");
        return output;
    }
}

Related

  1. addEscape(String a_unescaped)
  2. addEscapeCapacityToRegex(String inputRegex)
  3. addEscapeCharactersForSpaces(String s)
  4. addEscapeCharsToBackSlash(String code)
  5. addEscapes(String s)
  6. addEscapes(String str)