views:

49

answers:

2

In my app I want to remove numbers except characters a-z from string. How can I get only characters?

A: 

I found an answer:

NSString *originalString = @"(123) 123123 abc";

NSLog(@"%@", originalString);
NSMutableString *strippedString = [NSMutableString 
                                   stringWithCapacity:originalString.length];

NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet 
                           characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz"];

while ([scanner isAtEnd] == NO) {
    NSString *buffer;
    if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
        [strippedString appendString:buffer];

    } else {
        [scanner setScanLocation:([scanner scanLocation] + 1)];
    }
}

NSLog(@"%@", strippedString);
ksk
I'm no expert in xcode, but isn't there a simpler solution to perform that simple task?
ring0
+1  A: 
NSString *stringToFilter = @"filter-me";


    NSMutableString *targetString = [NSMutableString string];


    //set of characters which are required in the string......
    NSCharacterSet *okCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyz"];


    for(int i = 0; i < [stringToFilter length]; i++)
    {
        unichar currentChar = [stringToFilter characterAtIndex:i];
        if([okCharacterSet characterIsMember:currentChar]) 
        {
            [targetString appendFormat:@"%C", currentChar];
        }
    }


    NSLog(targetString);    


    [super viewDidLoad];
}

this was an answer given to me and works fine

Ranjeet Sajwan
please give link of that question from where you get it....otherwise good ans...
Online