Rajesh Panchal
Rajesh Panchal

Reputation: 1170

Extract alphabets, space and comma from string in android

I have a string mixed with numbers and alphabets as below, with that i am doing following replacement

String fullString = "08094 Williamstown, New Jersey";  
String extractedString = fullString .replaceAll("[^a-z^A-Z]", "");

the result i am getting is : WilliamstownNewJersey
but i want it like : Williamstown, New Jersey

How can i get it ?
Thank You.

Upvotes: 0

Views: 96

Answers (2)

rangalo
rangalo

Reputation: 5606

Your regex checks for non-alphabets this includes comma and spaces.

For this case you can try

String extractedString = fullString .replaceAll("\\d", "").trim();

or

String extractedString = fullString .replaceAll("[0-9]", "").trim();

You can check your regex online here.

http://www.regexplanet.com/advanced/java/index.html

Upvotes: 2

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5636

try this regex, hope will suffice your need.

([^a-zA-Z, ])

Upvotes: 0

Related Questions