Navi
Navi

Reputation: 1686

Extract substring from a string in iOS?

Is there any way to extract substring from a string like below

My real string is "NS09A" or "AB455A" but i want only "NS09" or "AB455" (upto the end of numeric part of original string).

How can i extract this?

I saw google search answers like using position of starting and endinf part of substring we can extract that ,But here any combination of "Alphabets+number+alphabets" .I need only " "Alphabets+number"

Upvotes: 1

Views: 4744

Answers (5)

kirti Chavda
kirti Chavda

Reputation: 3015

I tested this code:

   NSString *originalString = @"NS09A";

    // Intermediate
    NSString *numberString;
    NSString *numberString1;


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


    [scanner scanUpToCharactersFromSet:numbers intoString:&numberString];

    [scanner scanCharactersFromSet:numbers intoString:&numberString1];

    NSString *result=[NSString stringWithFormat:@"%@%@",numberString,numberString1];

    NSLog(@"Finally ==%@",result);

Hope it Help You

OUTPUT

Finally ==NS09

UPDATE:

NSString *originalString = @"[email protected]";

NSString *result;
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *cs1 = [NSCharacterSet characterSetWithCharactersInString:@"@"];

[scanner scanUpToCharactersFromSet:cs1 intoString:&result];

NSLog(@"Finally ==%@",result);

output:
Finally ==kirtimali

Upvotes: 2

Chaman Sharma
Chaman Sharma

Reputation: 636

Try This:-

NSString *str=@"ASRF12353FYTEW";
NSString *resultStr;
for(int i=0;i<[str length];i++){
    NSString *character = [str substringFromIndex: [str length] - i];
    if([character intValue]){
        resultStr=[str substringToIndex:[str length]-i+1];
        break;
    }
}
NSLog(@"RESUKT STRING %@",resultStr);

Upvotes: 3

Martin R
Martin R

Reputation: 540075

Perhaps not everybody will agree, but I like regular expressions. They allow to specify precisely what you are looking for:

NSString *string = @"AB455A";

// One or more "word characters", followed by one or more "digits":
NSString *pattern = @"\\w+\\d+";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                       options:0
                                     error:NULL];
NSTextCheckingResult *match = [regex firstMatchInString:string
                        options:NSMatchingAnchored
                          range:NSMakeRange(0, [string length])];
if (match != nil) {
    NSString *extracted = [string substringWithRange:[match range]];
    NSLog(@"%@", extracted);
    // Output: AB455
} else {
    // Input string is not of the expected form.
}

Upvotes: 4

Wain
Wain

Reputation: 119041

Use NSScanner and the scanUpToCharactersFromSet:intoString: method to specify which characters should be used to stop the parsing. This could be in a loop with some logic or it could be applied in conjunction with setScanLocation: if you already have a method of finding the start of each section you want to extract.


When using scanUpToCharactersFromSet:intoString: you are looking for the next invalid character. It doesn't need to be a 'special' character (in a unicode sense), just a known set of characters that aren't valid for the content you want. So, you might use:

[[NSCharacterSet characterSetWithCharactersInString:@"1234567890"] invertedSet]

Upvotes: 1

Pankaj Rathor
Pankaj Rathor

Reputation: 375

You can use - (NSString *)substringWithRange:(NSRange)aRange method on NSString class to get a substring extracted. Use NSMakeRange to create the NSRange object.

Upvotes: -3

Related Questions