Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

How to remove special characters from a string in Java?

Sun Jul 08, 2012 4:46 pm

Hello,
I need a way out in Java to remove all special characters present in a String. Somebody please give me ideas.
Regards.



Re: How to remove special characters from a string in Java?

Sun Jul 08, 2012 11:12 pm

what you think about this idea :

java code
public class Main {

    public static void main(String[] args) {
        String str="gasd@!dfas%";
        String newStr="";
          for (int i=0;i<str.length();i++)
          {
              //Ascci range for a-z A-Z
              if (str.charAt(i)>64&&str.charAt(i)<121)
              {
                    newStr+=str.charAt(i);
              }
          }

         System.out.println("String before filter "+str);
        System.out.println("String after filter "+newStr);

    }

}

 


Re: How to remove special characters from a string in Java?

Mon Jul 09, 2012 9:56 am

Thanks a lot. :-)

Re: How to remove special characters from a string in Java?

Mon Sep 17, 2012 9:58 am

That depends on what you define as special characters, but try replaceAll(...):
String result = yourString.replaceAll("[-+.^:,]","");

Post a reply
  Related Posts  to : How to remove special characters from a string in Java?
 Special Characters literals     -  
 Number of Occurrences of characters in a string     -  
 Remove all the vowels from a string     -  
 Unicode, ASCII, UTF-16, and UTF-8 characters in java     -  
 passing string value from java to .exe file     -  
 URL Characters and URL Encoding Values     -  
 continuously calculates the number of characters existing     -  
 Program to make a pyramid of characters based on input     -  
 Splitting a String Based on a Found String     -  
 recursive string reversal- reverse string     -