Newsletter |
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….
Java4s.java
public class Java4s { public static void main(String[] args) { String s = "(123)-456-7891"; s = s.replaceAll("[^a-zA-Z0-9]", ""); System.out.println(s); } }
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 .:: | ||
Comments
12 Responses to “How to Remove Unwanted Characters From String in Java”
In real time applications when to use multithreading.
Hi,
please provide android tutorials.
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?
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’
its good website and easily learn and solve the code…nice
its good website for beginner.Please add collection tutorial.
Hi please provide me core java and android real time scenario
Very good website easily we can solved.@…
Siva, Please provide some real good thread programs also. I saw, you explain very well.
How can i remove duplicate objects in a string.
can you please write one example program
hehe
s = s.replaceAll(“[^\\d]”,””);
is the same.
very good website it is very useful