tags:

views:

109

answers:

3

Hi All,

In my project i have string suppose NSString* str = @"$ 120.00";

From the above string i am getting every individual character, Now i have to get all integres in the string i.e, $ is not a integer so i dont want that, "1" is a integer i want that like this . How can i do that can any one help me

Thank you

+1  A: 

You can get a character set for digits and then use it to check your characters:

NSCharacterSet* digits = [NSCharacterSet decimalDigitCharacterSet];
if([digits characterIsMember: yourCharacter]) {
 ...
}
unbeli
+1  A: 

Unbeli's answer is probably the best for what it sounds like you want, which is an array of characters that happen to represent integers.

However, if your end goal is to reassemble all of the integer characters you've pulled out into a number, I'd suggest regular expressions. Cocoa doesn't have regex wrappers for replacement, but you should be able to use standard C <regex.h> code; Obj-C is a superset after all.

But that would give you 12000 in your example, as opposed to 120 which might be what you're after. In that case, I'd give [str intValue] a try.

Zind
A: 
// gcc foo.m -framework Foundation
int main()
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSString* str = @"$ 120.00";
    const char* p = [str cStringUsingEncoding:[NSString defaultCStringEncoding]];
    while( *p )
    {
        if( isdigit( *p ) )
        {
            NSLog(@"%c", *p );
        }
        p++;
    }
    [pool release];
}
zenerino