Paked - How to remove all special characters from a String in Java?


How to remove all special characters from a String in Java?

You will need to understand regular expressions in order to remove the unwanted characters. You can use [^A-Za-z] to allow only characters to appear in a string. Furthermore, you can use replaceAll method to replace or remove all unwanted characters from a string (see http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String).

Example:

 str.replaceAll("[^A-Za-z]","");

To learn more about regular expression see the following links:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

http://java.sun.com/docs/books/tutorial/essential/regex/

Your Answer




DISCLAIMER

Paked and the contributors are not responsible for any errors contained and are not liable for any damages resulting from the use of this material.  

Disclaimer


Home | Disclaimer | Advertise| Contact| Privacy Policy

 

Copyright © 2004-10 Paked.com. All rights reserved.

Note: Site best viewed at 1024 x 768 or higher screen resolution