'Finding first letter in NSString and counting backwards

I'm new to IOS, and was looking for some guidance.

I have a long NSString that I'm parsing out. The beginning may have a few characters of garbage (can be any non-letter character) then 11 digits or spaces, then a single letter (A-Z). I need to get the location of the letter, and get the substring that is 11 characters behind the letter to 1 character behind the letter.

Can anyone give me some guidance on how to do that?

Example: '!!2553072 C'

and I want : '53072 '



Solution 1:[1]

You can accomplish this with the regex pattern: (.{11})\b[A-Z]\b

The (.{11}) will grab any 11 characters and the \b[A-Z]\b will look for a single character on a word boundary, meaning it will be surrounded by spaces or at the end of the string. If characters can follow the C in your example then remove the last \b. This can be accomplished in Objective-C like so:

NSError *error;
NSString *example = @"!!2553072      C";
NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:@"(.{11})\\b[A-Z]\\b"
                              options:NSRegularExpressionCaseInsensitive
                              error:&error];

if(!regex)
{
    //handle error
}

NSTextCheckingResult *match = [regex firstMatchInString:example
                                                options:0
                                                  range:NSMakeRange(0, [example length])];
if(match)
{
    NSLog(@"match: %@", [example substringWithRange:[match rangeAtIndex:1]]);
}

Solution 2:[2]

You can use NSCharacterSets to split up the string, then take the first remaining component (consisting of your garbage and digits) and get a substring of that. For example (not compiled, not tested):

- (NSString *)parseString:(NSString *)myString {
    NSCharacterSet *letters = [NSCharacterSet letterCharacterSet];
    NSArray *components = [myString componentsSeparatedByCharactersInSet:letters];
    assert(components.count > 0);
    NSString *prefix = components[0]; // assuming relatively new Xcode
    return [prefix substringFromIndex:(prefix.length - 11)];
}

Solution 3:[3]

//to get rid of all non-Digits in a NSString
NSString *customerphone = CustomerPhone.text;
int phonelength = [customerphone length];
NSRange customersearchRange = NSMakeRange(0, phonelength);
for (int i =0; i < phonelength;i++)
{
    const unichar c = [customerphone characterAtIndex:i];
    NSString* onechar = [NSString stringWithCharacters:&c length:1];
    if(!isdigit(c))
    {
        customerphone = [customerphone stringByReplacingOccurrencesOfString:onechar withString:@"*" options:0 range:customersearchRange];
    }
}
NSString *PhoneAllNumbers = [customerphone stringByReplacingOccurrencesOfString:@"*" withString:@"" options:0 range:customersearchRange];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joe
Solution 2 Tim
Solution 3 Dr. Roger Webster