Convert String to Number

9 views
Skip to first unread message

Northwest Mobile Development LLC

unread,
Feb 13, 2012, 3:10:27 PM2/13/12
to pdx-cocoa...@googlegroups.com
Hi All:

I have a piece of code where I am stumped and was wondering what the best method would be.  So I have a textfield called phone that I want to pass.  However when the field is displayed it looks like (503) 555-1212.  How can I format the NSString so that it only passes the numbers in the following IBAction and ignoes any nonnumber character?
- (IBAction)openPhone:(id)sender;
NSURL *url = [NSURL URLWithString:phone.text];
    [[UIApplication sharedApplication] openURL:url];

    

    

}

Thanks!
----------------
William Frowine, PMP
Northwest Mobile Development LLC
in...@nwmobiledev.com


Janine Ohmer

unread,
Feb 13, 2012, 3:15:43 PM2/13/12
to pdx-cocoa...@googlegroups.com
There are a bunch of possible answers here:


You'll have to experiment to figure out which ones actually do work (as is often the case with StackOverflow answers).

janine

Justin R. Miller

unread,
Feb 13, 2012, 3:16:29 PM2/13/12
to pdx-cocoa...@googlegroups.com
On Feb 13, 2012, at 12:10 PM, Northwest Mobile Development LLC wrote:

> I have a piece of code where I am stumped and was wondering what the best method would be. So I have a textfield called phone that I want to pass. However when the field is displayed it looks like (503) 555-1212. How can I format the NSString so that it only passes the numbers in the following IBAction and ignoes any nonnumber character?
> - (IBAction)openPhone:(id)sender;
> NSURL *url = [NSURL URLWithString:phone.text];
> [[UIApplication sharedApplication] openURL:url];
> }

Something that's quite handy here is NSCharacterSet for specifying types of characters. Unfortunately the closest method that NSString has for what you need is stringByTrimmingCharactersInSet:, which could be used with something like [NSCharacterSet decimalDigitCharacterSet] to strip out the numbers -- which is the opposite of what you want.

But combining that with [NSCharacterSet characterSetWithCharactersInString] could get you there in two moves:

NSString *unwantedCharacters = [phone.text stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
NSString *finalPhoneNumber = [phone.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString: unwantedCharacters]];

--

Justin R. Miller
http://codesorcery.net

Northwest Mobile Development LLC

unread,
Feb 13, 2012, 3:26:12 PM2/13/12
to pdx-cocoa...@googlegroups.com
Thanks Justine & Janine! Looks like what I am looking for.

-Bill


----------------
William Frowine, PMP
Northwest Mobile Development LLC
in...@nwmobiledev.com

Michael

unread,
Feb 13, 2012, 4:40:56 PM2/13/12
to pdx-cocoa...@googlegroups.com
I'm still learning this and haven't used these classes, but I poked
around in the reference
docs and found the classes NSReularExpression and NSTextCheckingResult.
Within
NSTextCheckingResult I see methods that apply specifically to checking
for phone numbers
and a property of (NSString *)phoneNumber.

Has anyone used these methods or are they pretty obscure?

- Michael

On 2/13/2012 12:26 PM, Northwest Mobile Development LLC wrote:
> Thanks Justine& Janine! Looks like what I am looking for.

Janine Ohmer

unread,
Feb 13, 2012, 4:41:40 PM2/13/12
to pdx-cocoa...@googlegroups.com
I haven't used them but that doesn't make them obscure; I just haven't needed to do this yet.

janine

Northwest Mobile Development LLC

unread,
Feb 13, 2012, 4:58:03 PM2/13/12
to pdx-cocoa...@googlegroups.com
Hi I tried the following code but couldn't get it to work. Does anyone see where I am going wrong?

- (IBAction)openPhone:(id)sender {
NSString *originalPhoneNumber = phone.text;
NSCharacterSet *numbers = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
NSString *trimmedPhoneNumber = [originalPhoneNumber stringByTrimmingCharactersInSet:numbers];


NSURL *url = [NSURL URLWithString:trimmedPhoneNumber];
[[UIApplication sharedApplication] openURL:url];


}

----------------
William Frowine, PMP
Northwest Mobile Development LLC
in...@nwmobiledev.com

Justin R. Miller

unread,
Feb 13, 2012, 5:03:03 PM2/13/12
to pdx-cocoa...@googlegroups.com
No tel:// protocol?

Cody Garvin

unread,
Feb 13, 2012, 5:24:19 PM2/13/12
to pdx-cocoa...@googlegroups.com
Another thing you may try is RegexKitLite if you know Regular expressions :)

- Cody

Northwest Mobile Development LLC

unread,
Feb 13, 2012, 5:54:43 PM2/13/12
to pdx-cocoa...@googlegroups.com
Cody:

Sorry don't know the regex stuff.

Thanks thought...

----------------
William Frowine, PMP
Northwest Mobile Development LLC
in...@nwmobiledev.com


Northwest Mobile Development LLC

unread,
Feb 14, 2012, 3:45:11 AM2/14/12
to pdx-cocoa...@googlegroups.com
HI Guys:

Thanks for all of the help.  I found a code snippit that would actually do what I wanted and just got it to work.  I used the scanner to scan the items I wanted to allow.  So here is the code for those who are interested.

- (IBAction)openPhone:(id)sender {

   

    //the String with the original text  
    NSString *unfilteredString = phone.text;  
    //initialize a string that will hold the result  
    NSMutableString *resultString = [NSMutableString stringWithCapacity:unfilteredString.length];  

    

    NSScanner *scanner = [NSScanner scannerWithString:unfilteredString];  
    //define the allowed characters, here only numbers from one to three, equal and plus  
    NSCharacterSet *allowedChars = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];  

    

    while ([scanner isAtEnd] == NO) {  
        NSString *buffer;  
        if ([scanner scanCharactersFromSet:allowedChars intoString:&buffer]) {  
            [resultString appendString:buffer];       
        } else {  
            [scanner setScanLocation:([scanner scanLocation] + 1)];  
        }  
    }  
    //Print out the result String, will be 1+2=3     
    //NSLog (@"Result: %@", resultString);  
    //Posted by stphn at 4:29 AM

    

    

    

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: @"tel:%@", resultString]]];
}

----------------
William Frowine, PMP
Northwest Mobile Development LLC
in...@nwmobiledev.com


On Feb 13, 2012, at 2:24 PM, Cody Garvin wrote:

Tyler Morrison

unread,
Feb 14, 2012, 4:48:05 PM2/14/12
to pdx-cocoa...@googlegroups.com
BTW: The documentation for stringByTrimmingCharactersInSet says "Returns a new string made by removing from both ends of the receiver characters contained in a given character set." So that's why it didn't work for you - not going to remove the ones in the middle.

Justin R. Miller

unread,
Feb 17, 2012, 9:29:35 PM2/17/12
to pdx-cocoa...@googlegroups.com
Good call -- I forget about that. "Trim" has a meaning!

JM

Northwest Mobile Development LLC

unread,
Feb 17, 2012, 10:24:13 PM2/17/12
to pdx-cocoa...@googlegroups.com
Thanks. I actually figured that out.

----------------
William Frowine, PMP
Northwest Mobile Development LLC
in...@nwmobiledev.com
Reply all
Reply to author
Forward
0 new messages