Delete all special characters in java

Possible duplicate:
Replacing all non-alphanumeric characters with blank lines

import java.util.Scanner; import java.util.regex.*; public class io{ public static void main(String args[]){ Scanner scan = new Scanner(System.in); String c; if((c=scan.nextLine())!=null) { Pattern pt = Pattern.compile("[^a-zA-Z0-9]"); Matcher match= pt.matcher(c); while(match.find()){ c=c.replace(Character.toString(c.charAt(match.start())),""); } System.out.println(c); } } } 

Case 1

 Input : hjdg$h&jk8^i0ssh6 Expect : hjdghjk8i0ssh6 Output : hjdgh&jk8^issh6 

Case 2

 Input : hjdgh&jk8i0ssh6 Expect : hjdghjk8i0ssh6 Output : hjdghjk8i0ssh6 

Case 3

 Input : hjdgh&j&k8i0ssh6 Expect : hjdghjk8i0ssh6 Output : hjdghjki0ssh6 

Can someone please help me figure out what is wrong with my code logic?

+7
source share
3 answers

use [\\W+] or "[^a-zA-Z0-9]" as a regular expression to match any special characters, and use String.replaceAll (regex, String) to replace a charecter with an empty string. remember how the first arg from String.replaceAll is a regular expression that you need to avoid by using a backslash to treat em as a charcter literal.

  String c= "hjdg$h&jk8^i0ssh6"; Pattern pt = Pattern.compile("[^a-zA-Z0-9]"); Matcher match= pt.matcher(c); while(match.find()) { String s= match.group(); c=c.replaceAll("\\"+s, ""); } System.out.println(c); 
+19
source

You can read lines and replace all special characters safely this way.
Keep in mind that if you use \\W , you will not replace underscores.

 Scanner scan = new Scanner(System.in); while(scan.hasNextLine()){ System.out.println(scan.nextLine().replaceAll("[^a-zA-Z0-9]", ""); } 
+11
source

Your problem is that the indexes returned by match.start() correspond to the character position, as it was in the original line when you matched it; however, when you rewrite the string c each time, these indices become invalid.

The best approach to solve this problem is to use replaceAll , for example:

  System.out.println(c.replaceAll("[^a-zA-Z0-9]", "")); 
+3
source

All Articles