Get all updates of Java4s
Newsletter

How to Remove Unwanted Characters From String in Java

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….

Java4s.java

Output

1234567891

Explanation

  • 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 .::

Java4s_Author
- Full Stack Java/J2EE & UI Web 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.

Comments

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:

    Hi,
    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:

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

  12. bhanu says:

    very good website it is very useful

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Spring-Boot-tutorials Hibernate tutorials Spring tutorials Struts tutorials Servlets tutorials Ajax tutorials Log4j tutorials jQuery tutorials Json tutorials Core Java tutorials Web Services tutorials AngularJs tutorials
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Contact | About Us | Privacy Policy | Advertise With Us

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