Question

I have the following style string passed in:

 <p><iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F79039882"></iframe></p> 

and

<p><iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F82229979&amp;color=ff6600&amp;auto_play=true&amp;show_artwork=true"></iframe></p>

I require to filter out the strings "79039882" or "82229979" from the given strings.

Currently I am using the following code:

NSString * temp1;
NSScanner *scanner = [NSScanner scannerWithString:label];
            [scanner scanUpToString:@"%2Ftracks%2F" intoString:nil];
            [scanner scanString:@"%2Ftracks%2F" intoString:nil];
            [scanner scanUpToString:@"&" intoString:&temp1];

Unfortunately it fails on the strings that are formatted as the first is. How would I be able to account for both types?

When the first style of string is passed in the temp1 result is this following:

79039882"></iframe></p>
Was it helpful?

Solution

Since you want only digits, use an NSCharacterSet that describes that, along with scanCharactersFromSet:intoString:

// Don't create this more than once if possible.
NSCharacterSet * decimalCharSet = [NSCharacterSet decimalDigitCharacterSet];
//...    
[scanner scanCharactersFromSet:decimalCharSet
                    intoString:&temp1];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top