sanjay
sanjay

Reputation: 2620

How to format and unformat phone number in android

Im doing an application in android which needs to enter phone number in formatted way like (xxx)xxx-xxx-x.

I used code as

EditText inputField = (EditText) findViewById(R.id.inputfield);
inputField.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); 

Its working well.But when i click a button.,i need to get that phone number in normal format like xxxxxxxxxx.How could i do that? Thanks.

Upvotes: 2

Views: 2327

Answers (3)

stinepike
stinepike

Reputation: 54672

use

String number = inputNumber.replaceAll("\\D","");

Note: \D matches a character that is not a digit

Upvotes: 7

FabianCook
FabianCook

Reputation: 20557

To get the text in xxxxxxxxx form you can use a regEx:

String value = string.replaceAll("[0-9]","");

Upvotes: 0

wazaminator
wazaminator

Reputation: 245

if your number is in a string,you can use a String parser

String tel = (123)123-123-1

String tmp = tel.substring(tmp.indexOf("(")+1,tmp.indexOf(")"));
String[] result = tmp.split("-");
for(int i=0;i< result.size();i++){
   String tel= tel +result[i];
}

Upvotes: 0

Related Questions