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"

2
  • is string always "alpha", "numeric", "alpha"? Commented Aug 1, 2013 at 10:40
  • no ,some cases it will be like that ,in normal case its like "alpha"+ "numeric" Commented Aug 1, 2013 at 10:45

5 Answers 5

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.
}
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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 = @"[email protected]";

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

[scanner scanUpToCharactersFromSet:cs1 intoString:&result];

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

output:
Finally ==kirtimali

1 Comment

@Kirti Mali How to substring when comes to Mail validation. I like to get substring upto @. Ex: [email protected] means then i want only mailname. Can you edit your post with this solution?
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]

1 Comment

can i mention that am stopping with a number instead of special character
-3

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

4 Comments

Why a down vote? It answers the questions to extract the substring.
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.
@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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.