Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone sdk - Remove all numbers except for characters a-z from a string

Tags:

string

iphone

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

like image 852
Saikiran Komirishetty Avatar asked Oct 14 '10 11:10

Saikiran Komirishetty


2 Answers

This is the short answer which doesnt need any lengthy coding

NSString *newString = [[tempstr componentsSeparatedByCharactersInSet:
                            [[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@""];`

swift 3:

(tempstr.components(separatedBy:NSCharacterSet.letters.inverted)).joined(separator: "")

eg:

("abc123".components(separatedBy:NSCharacterSet.letters.inverted)).joined(separator: "")
like image 102
Saikiran Komirishetty Avatar answered Sep 22 '22 06:09

Saikiran Komirishetty


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

like image 31
Ranjeet Sajwan Avatar answered Sep 24 '22 06:09

Ranjeet Sajwan