import org.apache.commons.lang.StringEscapeUtils; public class testEscapeHTML{ public static void main(String args[]){ String testStr = "< > \" &"; System.out.println("Original : " + testStr); System.out.println("Escaped : " + StringEscapeUtils.escapeHtml(testStr)); } }
or
This asks the regular expression parser to ignore any special characters in the string.
This is also very simple as the first method.
All you have to do is to put “\Q” in front of the string and “\E” at the end of the string.
Example : " Some Special characters (!@#$%^&*()?":;)" =>
"\Q Some Special characters (!@#$%^&*()?":;)\E"
This is however already available as a function in java
String escapedString=java.util.regex.Pattern.quote(myStringToEscape)
You can use this method to make parts of a string escape as a regular expression.
No comments:
Post a Comment