How to Remove Unwanted Characters From String in Java

Core Java » on Sep 28, 2012 { 12 Comments } By Sivateja

Friends, sometimes we may need to remove some unwanted characters from our strings, some thing if we have mobile number for example (123)-456-7891, i want to remove ‘ and () ‘ symbol before storing this field in the database then ?

Just use this simple code….

public class Java4s
  public static void main(String[] args)
    String s = "(123)-456-7891";
    s = s.replaceAll("[^a-zA-Z0-9]", "");





  • See my string is (123)-456-7891,  so in line number 6, i have written one expression which will removes every thing except these characters  ‘a-z, A-Z,0-9
  • Actually replaceAll() is a method in String class, i am passing 2 parameters, 1st parameter is the expression and second is to replace with what ?, in our case am replacing ‘-,),(‘ with nothing, so i have given “”, if want you can pass some characters also try.



You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


12 Responses to “How to Remove Unwanted Characters From String in Java”
  1. rajesh says:

    In real time applications when to use multithreading.

  2. Hemant says:

    please provide android tutorials.

  3. Prasad says:

    if i used String in my program,and i applied modifications on string then may be i get memory wastage then only we are useing string why?

  4. suresh says:

    hi, i have attend one interview asking this program can’t understand this requirements plz send this code

    Input:consider a file contain a list of 100,000 licence plate numbers which follow the format ABC-123 and could be value rangeing from AAA-001 to ZZZ-009
    This data is expreses to be read-in and stored in memory based on following requirements
    *The list can be reconstructed using the stored data.Original sequence does not need to be maintain.
    *perform searches effectiually on the store data using the first 3 of the numbers as given below
    Qn:list all licence strats with ‘MMM’

  5. Aaftab says:

    its good website and easily learn and solve the code…nice

  6. Deepak says:

    its good website for beginner.Please add collection tutorial.

  7. Sattibabu says:

    Hi please provide me core java and android real time scenario

  8. Mammu says:

    Very good website easily we can solved.@…

  9. Avinash says:

    Siva, Please provide some real good thread programs also. I saw, you explain very well.

  10. Narendra says:

    How can i remove duplicate objects in a string.
    can you please write one example program

  11. chomroeun says:

    s = s.replaceAll(“[^\\d]”,””);
    is the same.

  12. bhanu says:

    very good website it is very useful


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.