Scan from last instance of character to end of string using NSScanner

Posted by Virgil Disgr4ce on Stack Overflow See other posts from Stack Overflow or by Virgil Disgr4ce
Published on 2010-03-20T01:36:30Z Indexed on 2010/03/20 1:41 UTC
Read the original article Hit count: 295

Filed under:
|

Given a string such as: "new/path - path/path/03 - filename.ext", how can I use NSScanner (or any other approach) to return the substring from the last "/" to the end of the string, i.e., "03 - filename.ext"? The code I've been trying to start with is:

while ([fileScanner isAtEnd] == NO){
    slashPresent = [fileScanner scanUpToString:@"/" intoString:NULL];
    if (slashPresent == YES) {
        [fileScanner scanString:@"/" intoString:NULL];
        lastPosition = [fileScanner scanLocation];
    }
    NSLog(@"fileScanner position: %d", [fileScanner scanLocation]);
    NSLog(@"lastPosition: %d", lastPosition);       
}

...and this results in a seg fault after scanning to the end of the string! I'm not sure why this isn't working. Ideas? Thanks in advance!

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa