StringEscapeUtils.escapeJava


  • 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.



  1.  package com.utilitySample;
  2.  import org.apache.commons.lang.StringUtils;

  3.  public class StringUtility {
  4.  public static void main(String[] args) {
  5.    String inputStr = "sample jav\ta escape";
  6.    System.err.println("After escape ::"+StringEscapeUtils.escapeJava(inputStr));
  7.    System.err.println("After unescape ::"+StringEscapeUtils.unescapeJava(inputStr));
  8.    }
  9.  }



 Output :
 After escape ::sample jav\ta escape
 After unescape ::sample jav a escape

No comments:

Post a Comment