1

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"

Navi
  • 1,696
  • 2
  • 17
  • 36

5 Answers5

4

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.
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
3

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);
Chaman Sharma
  • 641
  • 5
  • 10
2

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 = @"kirtimali@gmail.com";

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

[scanner scanUpToCharactersFromSet:cs1 intoString:&result];

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

output:
Finally ==kirtimali
Mohd Sadham
  • 435
  • 3
  • 19
kirti Chavda
  • 3,029
  • 2
  • 17
  • 29
  • @Kirti Mali How to substring when comes to Mail validation. I like to get substring upto `@`. `Ex: mailname@gmail.com` means then i want only `mailname`. Can you edit your post with this solution? – Mohd Sadham Apr 19 '14 at 05:27
1

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]
Wain
  • 118,658
  • 15
  • 128
  • 151
-3

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

Pankaj Rathor
  • 375
  • 1
  • 3
  • 9
  • Why a down vote? It answers the questions to extract the substring. – Pankaj Rathor Aug 01 '13 at 10:47
  • I think, the votedown was made, bcoz your answer tells to extract the substring with the use of a range. In this question, there is no particular range. There is need to first find the range, then perform trimming operations, upto it. – Hitesh Aug 01 '13 at 10:54
  • @PankajRathor i dont know why u got down votes,but for the effort to write a answer here i 'll add one up vote now. – Navi Aug 01 '13 at 12:11
  • @Navi You're not supposed to upvote answers for the sake of it being an answer. You upvote if you think it's a good answer. – Jens Bergvall Aug 01 '13 at 13:05