Class StringEscapeUtils

java.lang.Object
com.jbstrap.core.utils.StringEscapeUtils

public final class StringEscapeUtils extends Object
Since:
4.0
Author:
JBStrap
  • Method Details

    • escapeJava

      public static String escapeJava(String str) throws Exception

      Escapes the characters in a String using Java String rules.

      Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)

      So a tab becomes the characters '\\' and 't'.

      The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.

      Example:

       input string: He didn't say, "Stop!"
       output string: He didn't say, \"Stop!\"
       
      Parameters:
      str - String to escape values in, may be null
      Returns:
      String with escaped values, null if null string input
      Throws:
      Exception - Exception
    • escapeJava

      public static void escapeJava(Writer out, String str) throws IOException

      Escapes the characters in a String using Java String rules to a Writer.

      A null string input has no effect.

      Parameters:
      out - Writer to write escaped string into
      str - String to escape values in, may be null
      Throws:
      IllegalArgumentException - if the Writer is null
      IOException - if error occurs on underlying Writer
      See Also:
    • escapeJavaScript

      public static String escapeJavaScript(String str) throws Exception

      Escapes the characters in a String using JavaScript String rules.

      Escapes any values it finds into their JavaScript String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)

      So a tab becomes the characters '\\' and 't'.

      The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote must be escaped.

      Example:

       input string: He didn't say, "Stop!"
       output string: He didn\'t say, \"Stop!\"
       
      Parameters:
      str - String to escape values in, may be null
      Returns:
      String with escaped values, null if null string input
      Throws:
      Exception - Exception
    • escapeJavaScript

      public static void escapeJavaScript(Writer out, String str) throws Exception

      Escapes the characters in a String using JavaScript String rules to a Writer.

      A null string input has no effect.

      Parameters:
      out - Writer to write escaped string into
      str - String to escape values in, may be null
      Throws:
      IllegalArgumentException - if the Writer is null
      IOException - if error occurs on underlying Writer
      Exception
      See Also:
    • unescapeJava

      public static String unescapeJava(String str) throws Exception

      Unescapes any Java literals found in the String. For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.

      Parameters:
      str - the String to unescape, may be null
      Returns:
      a new unescaped String, null if null string input
      Throws:
      Exception - Exception
    • unescapeJava

      public static void unescapeJava(Writer out, String str) throws Exception

      Unescapes any Java literals found in the String to a Writer.

      For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.

      A null string input has no effect.

      Parameters:
      out - the Writer used to output unescaped characters
      str - the String to unescape, may be null
      Throws:
      IllegalArgumentException - if the Writer is null
      IOException - if error occurs on underlying Writer
      Exception
    • unescapeJavaScript

      public static String unescapeJavaScript(String str) throws Exception

      Unescapes any JavaScript literals found in the String.

      For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.

      Parameters:
      str - the String to unescape, may be null
      Returns:
      A new unescaped String, null if null string input
      Throws:
      Exception - Exception
      See Also:
    • unescapeJavaScript

      public static void unescapeJavaScript(Writer out, String str) throws Exception

      Unescapes any JavaScript literals found in the String to a Writer.

      For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.

      A null string input has no effect.

      Parameters:
      out - the Writer used to output unescaped characters
      str - the String to unescape, may be null
      Throws:
      IllegalArgumentException - if the Writer is null
      IOException - if error occurs on underlying Writer
      Exception
      See Also: