If we have below two formats for phone number in string array.
YYYXXXZZZ , YYY-XXX-ZZZZ
and we want to normalize these phone numbers to :
XXX-YYY-ZZZZ.
How can we do it?
If we have below two formats for phone number in string array.
YYYXXXZZZ , YYY-XXX-ZZZZ
and we want to normalize these phone numbers to :
XXX-YYY-ZZZZ.
How can we do it?
Just extract the substrings based upon the detected format.
if (!p.contains("-")) {
normalized = p.substring(3, 6) + "-" + p.substring(0, 3) + "-" + p.substring(6);
} else {
normalized = p.substring(4, 7) + "-" + p.substring(0, 3) + "-" + p.substring(8);
}
I believe you accidentally inserted an extra "Y" for your expected result. EDIT: It has been corrected
public void formatNumber (String number) {
String formatted=number.substring(3,6)+"-"+number.substring(0,3)+"-"+ number.substring(6);
return formatted;
}