phone number format for iPhone - ios

I have to make calls programmatically in my iPhone app.
I have set of numbers in different countries with different formatting - braces, dots, spaces, "+" sign.
Can I simply remove all of this and left only numbers?
for example:
+1-(609) 452-8401 => 16094528401 // usa
+49(0)89.439 => 49089439 // germany
+1-(949)586-1250 => 19495861250 // los angeles, usa
Will it be correct?

try this:-
NSMutableString *str1=[[NSMutableString alloc] initWithString:telephoneString];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:#"(" withString:#""]];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:#")" withString:#""]];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:#"-" withString:#""]];
[str1 setString:[str1 stringByReplacingOccurrencesOfString:#" " withString:#""]];
telephoneString = [#"tel://" stringByAppendingString:str1];
[str1 release];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:telephoneString]];

Related

stringByReplacingOccurrencesOfString is not working for space i.e, " " in objective c

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty;
{
[self dismissViewControllerAnimated:YES completion: nil];
CNLabeledValue *phoneNumberValue = contactProperty.value;
NSString *contactString = [phoneNumberValue valueForKey:#"_stringValue"];
contactString = [contactString stringByReplacingOccurrencesOfString:#"-" withString:#""];
contactString = [contactString stringByReplacingOccurrencesOfString:#" " withString:#""]; // This line of code is not working properly
contactString = [contactString stringByReplacingOccurrencesOfString:#"(" withString:#""];
contactString = [contactString stringByReplacingOccurrencesOfString:#")" withString:#""];
txtRecipient.text = contactString;
}
This is my code. I am using contactPicker to pick a contact from phonebook. Then I am storing it in to a string variable. After that I am removing dashes, brackets and spaces from string value by using stringByReplacingOccurrencesOfString. Every thing is working fine except for this line:
contactString = [contactString stringByReplacingOccurrencesOfString:#" " withString:#""];
After this line of code contactString remains the same i.e, spaces didn't get removed from the string. I also tried componentsSeparatedByString function but its returning only 1 character. i.e,
NSArray *components = [dateString componentsSeparatedByString:#" "];
components.length is returning 1.
Hope you understand my question. Is there any other way of removing spaces from a string? Any kind of help would be appreciable. Thanks.
Look like this is not a standard space.
Try this:
NSMutableCharacterSet* set = [NSMutableCharacterSet whitespaceCharacterSet];
[set addCharactersInString:#"()-"];
NSMutableString * contactString = [[phoneNumberValue valueForKey:#"_stringValue"] mutableCopy];
NSRange range;
while ((range = [contactString rangeOfCharacterFromSet:set]).location!=NSNotFound) {
[contactString deleteCharactersInRange:range];
}

Remove formatting from a string: “(123) 456-7890” => “1234567890” in objective c

I have a string of phone number with format (123)-(456)-7890 but how can i convert to the following form 1234567890?
Try this
NSString *numberString = [[mixedString componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:#""];
It will give digits from a string.
stringByReplacingOccurrencesOfString supports also regular expression
NSString *phoneNumber = #"(123)-(456)-7890";
NSString *filteredPhoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"[ ()-]"
withString:#""
options:NSRegularExpressionSearch
range:NSMakeRange(0, phoneNumber.length)];
NSLog(#"%#", filteredPhoneNumber);
Put all characters to be ignored between the brackets in the first parameter.
An alternative regex is #"[^\\d+]" which means ignore all non-digit characters
NSString *str = #"(123)-(456)-7890";
str = [str stringByReplacingOccurrencesOfString: #"(" withString:#""];
str = [str stringByReplacingOccurrencesOfString: #")" withString:#""];
str = [str stringByReplacingOccurrencesOfString: #"-" withString:#""];
NSLog(#"Output = %#",str);
Output = 1234567890
You also replacing more character in Single Line of coding using stringByReplacingOccurrencesOfString
NSString *YourString = #"(123)-(456)-7890";
YourString = [[[YourString stringByReplacingOccurrencesOfString:#"(" withString:#""] stringByReplacingOccurrencesOfString:#")" withString:#""] stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSLog(#"YourString == > %#",YourString);
YourString == > 1234567890
Use this code,
NSString *valString = #"(123)456-7890";
NSString* phoneStr=[[[valString stringByReplacingOccurrencesOfString:#"(" withString:#""] stringByReplacingOccurrencesOfString:#")" withString:#""] stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSLog(#"phone String %#",phoneStr); // finally you get phone String 1234567890
its working for me, hope its helpful
If you want your code to work outside the USA, use Google's telephony library. Formatting and extracting phone numbers is difficult. I wouldn't even try doing it myself by hand.

NSString remove a non braking space

Let's say I get a string "123 4,56" from the following code (I have a Russian local at the moment which has comma as a default separator rather than a dot), how do I remove the space from it which in reality is a non breaking space and the standard code like [str stringByReplacingOccurrencesOfString:#" " withString:#""]; will not work:
NSString* amount = #"1234.56";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_UK"]];
NSNumber *num = [formatter numberFromString:amount];
NSString* str = [NSString localizedStringWithFormat:#"%.2F", [num doubleValue]];
NSLog(#"Str Value: %#", str);
NSString *newStr = [str stringByReplacingOccurrencesOfString:#" " withString:#""];
NSLog(#"New Str Value: %#", newStr);
The output I get is:
Str Value: 1 234,56
New Str Value: 1 234,56
The problem I'm trying to solve is I get a string from the server which is a currency (i.e. 123.45) and I need to display that in the UITextField. The problem is that I can't just display the value as it comes from the server because it is dependant on the user local. If the user has a UK local that works fine, however if the user has a Russian local I need to display a comma instead of a dot, thus basically what the code above does. The issue I however get is that after converting the string from one local to another there is an annoying space added to it that I'm trying to get rid of.
Solution based on Doro answer
[str stringByReplacingOccurrencesOfString:#"\u00a0" withString:#""];
It sounds strange, did you check that that was exactly whitespace character?
i can offer this snippet for striping input characters using scanner:
- (NSString*) stripInputValue: (NSString*) inputValue
{
NSMutableString *strippedString = [NSMutableString
stringWithCapacity:inputValue.length];
NSScanner *scanner = [NSScanner scannerWithString:inputValue];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSString *decimalSymbol = [formatter decimalSeparator];
NSCharacterSet *validCharacters = [NSCharacterSet characterSetWithCharactersInString:[#"1234567890" stringByAppendingString:decimalSymbol]];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:validCharacters intoString:&buffer]) {
[strippedString appendString:buffer];
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
return strippedString;
}
Be careful - if you want to support multiply locales - some locales uses '.' as separator, some ','. You can check this programmatically, if needed.
EDIT
Also please note that iOS doesn't use a space as a separator but a non-breaking space (U+00A0) for localizedStringWithFormat: so that is your problem.
Hope this helps.
The first line should have worked
NSString* newStr = [str stringByReplacingOccurrencesOfString:#" " withString:#""];
But just in case it didn't, I have another way for you:
NSString* newStr2 = [[str componentsSeparatedByString:#" "] componentsJoinedByString:#""];
UPDATE:
Instead of playing with the locale manually, you should use dynamic locale method for your requirement.
Replace: [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_UK"]];
By:
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setLocale:[NSLocale currentLocale]];

Removing Specific Characters from NSString

For example if i had a string like
NSString *myString = #"A B C D E F G";
and I want to remove the spaces, and get a string out like "ABCDEFG".
I could use
NSString *stringWithoutSpaces = [myString stringByReplacingOccurrencesOfString:#" " withString:#""];
However, for my application I am loading in phone numbers from the address book
and the numbers often have a different formatting layout.
I'm wondering if I have a phone number stored in a string like
+1-(937)673-3451 how would I go about removing only the first "1" the "+" the "-" and the "(" ")".
Overall, I would like to know if it is possible to remove the first "1" without removing the last one in the string?
There are a lot of ways to do this. Here's one:
NSString *phoneNumber = #"+1-(937)673-3451";
NSCharacterSet *removalCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"+()-"];
NSArray *components = [phoneNumber componentsSeparatedByCharactersInSet:removalCharacterSet];
NSString *simplifiedPhoneNumber = [components componentsJoinedByString:#""];
NSRange firstCharacterRange = NSMakeRange(0, 1);
if ([[simplifiedPhoneNumber substringWithRange:firstCharacterRange] isEqualToString:#"1"]) {
simplifiedPhoneNumber = [simplifiedPhoneNumber stringByReplacingCharactersInRange:firstCharacterRange withString:#""];
}
NSLog(#"Full phone number: %#", phoneNumber);
NSLog(#"Simplified phone number: %#", simplifiedPhoneNumber);
But really you want to use a library that knows what a phone number is supposed to look like, like libPhoneNumber.

Calling Through App with openUrl

I've been trying to use the openUrl function is iOS to dial phone numbers from my app, but it's not going through, the numbers from the response have white spaces, and I've tried to remove it but when I NSLog it's not removing it
(void)phone:(id)sender{
NSString *phone = [[information valueForKeyPath:#"place_detail"] objectForKey:#"phone"];
[phone stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *dial = [NSString stringWithFormat:#"tel://%#", phone];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:dial]];
}
Try replacing this:
[phone stringByReplacingOccurrencesOfString:#" " withString:#""];
With this:
phone = [phone stringByReplacingOccurrencesOfString:#" " withString:#""];
Use this method to remove all forms of white space:
NSArray* words = [yourString componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]];
NSString* nospacestring = [words componentsJoinedByString:#""];
This is advantageous because it removes not only the space character.
Next just call with the following method:
NSString *value=#"your number";
NSURL *url = [[ NSURL alloc ] initWithString:[NSString stringWithFormat:#"tel://%#",value]];
[[UIApplication sharedApplication] openURL:url];

Resources