Reputation: 361
I need to convert a phone number into a numeric string. For example +1222333-456789 to 1222333456789.
What is the best way to this with salesforce apex?
Upvotes: 0
Views: 2588
Reputation: 21
This can be accomplished with a regular expression:
String phoneNumber = '(987) 654-3210';
// Remove non-digit characters
String phoneNumberDigits = phoneNumber.replaceAll('\\D+','');
long phoneNumberInt = Long.parseLong(phoneNumberDigits);
Upvotes: 0
Reputation: 361
For those who need a similar thing, this is how I did it:
if (num.isNumeric()) {
return num;
}
else {
String n = '';
for (Integer i=0; i<num.length(); i++) {
if (num.substring(i, i+1).isNumeric()) {
n += num.substring(i, i+1);
}
}
return n;
}
Feel free to advice me how I can improve the code.
Upvotes: 1