snippetjavaMinor
Bring phone numbers in consistent format
Viewed 0 times
formatconsistentbringnumbersphone
Problem
I'm using this code to bring my phone numbers in a consistent format.
Desired:
Possible patterns:
This is what I use:
For some reason I'm not happy with it though. I hope there is somebody to tell me, whether my code is written properly!
Desired:
+(country code)phone numberPossible patterns:
01721234567 -> change to desired pattern00491234567 -> change to desired pattern+4912345678 -> do nothing, already desired patternThis is what I use:
String number = allContactNumbers.get(i).get(j);
number = number.replaceAll("[^+0-9]", ""); // All weird characters such as /, -, ...
String country_code = getResources().getString(R.string.countrycode_de);
if (number.substring(0, 1).compareTo("0") == 0 && number.substring(1, 2).compareTo("0") != 0) {
number = "+" + country_code + number.substring(1); // e.g. 0172 12 34 567 -> + (country_code) 172 12 34 567
}
number = number.replaceAll("^[0]{1,4}", "+"); // e.g. 004912345678 -> +4912345678For some reason I'm not happy with it though. I hope there is somebody to tell me, whether my code is written properly!
Solution
There are two aspects to this, the general design, and the implementation.
Implementation
The entire things should be extracted as a function. The first line of code:
indicates that this code is being run inside a loop (i,j). You need to extract it to be:
and the function would look something like:
Right, about that function, putting your code in it ends up with:
Design
OK, about the design.... I believe this is a problem. Handling phone numbers is much more complicated than what you have.... it is really a challenging problem.
It is easy enough to strip off the junk, but it gets hard really fast. For example, this is the the Queen of England's land-line number (not kidding):
As a Canadian, the international dialing code for this would be:
How will your script translate that in to:
I think you need to revise your plan for this problem, it is more complicated than you realize.
Implementation
The entire things should be extracted as a function. The first line of code:
String number = allContactNumbers.get(i).get(j);indicates that this code is being run inside a loop (i,j). You need to extract it to be:
String number = normalizePhoneNumber(allContactNumbers.get(i).get(j));and the function would look something like:
public String normalizePhoneNumber(String number) {
......
return normalized;
}Right, about that function, putting your code in it ends up with:
public String normalizePhoneNumber(String number) {
number = number.replaceAll("[^+0-9]", ""); // All weird characters such as /, -, ...
String country_code = getResources().getString(R.string.countrycode_de);
if (number.substring(0, 1).compareTo("0") == 0 && number.substring(1, 2).compareTo("0") != 0) {
number = "+" + country_code + number.substring(1); // e.g. 0172 12 34 567 -> + (country_code) 172 12 34 567
}
number = number.replaceAll("^[0]{1,4}", "+"); // e.g. 004912345678 -> +4912345678
return number;
}Design
OK, about the design.... I believe this is a problem. Handling phone numbers is much more complicated than what you have.... it is really a challenging problem.
It is easy enough to strip off the junk, but it gets hard really fast. For example, this is the the Queen of England's land-line number (not kidding):
Public Information Officer
Buckingham Palace
London SW1A 1AA
Tel (during 9am - 5pm (GMT) Monday to Friday): (+44) (0)20 7930 4832. Please note, calls to this number may be recorded.As a Canadian, the international dialing code for this would be:
01144207934832How will your script translate that in to:
+442079305832I think you need to revise your plan for this problem, it is more complicated than you realize.
Code Snippets
String number = allContactNumbers.get(i).get(j);String number = normalizePhoneNumber(allContactNumbers.get(i).get(j));public String normalizePhoneNumber(String number) {
......
return normalized;
}public String normalizePhoneNumber(String number) {
number = number.replaceAll("[^+0-9]", ""); // All weird characters such as /, -, ...
String country_code = getResources().getString(R.string.countrycode_de);
if (number.substring(0, 1).compareTo("0") == 0 && number.substring(1, 2).compareTo("0") != 0) {
number = "+" + country_code + number.substring(1); // e.g. 0172 12 34 567 -> + (country_code) 172 12 34 567
}
number = number.replaceAll("^[0]{1,4}", "+"); // e.g. 004912345678 -> +4912345678
return number;
}Public Information Officer
Buckingham Palace
London SW1A 1AA
Tel (during 9am - 5pm (GMT) Monday to Friday): (+44) (0)20 7930 4832. Please note, calls to this number may be recorded.Context
StackExchange Code Review Q#46125, answer score: 7
Revisions (0)
No revisions yet.