1

我传入了以下样式字符串:

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

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

我需要从给定的字符串中过滤掉字符串“79039882”或“82229979”。

目前我正在使用以下代码:

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

不幸的是,它在第​​一个格式化的字符串上失败了。我将如何解释这两种类型?

当在 temp1 中传递第一种字符串样式时,结果如下:

79039882"></iframe></p>
4

1 回答 1

2

由于您只需要数字,因此请使用NSCharacterSet描述该数字的scanCharactersFromSet:intoString:

// Don't create this more than once if possible.
NSCharacterSet * decimalCharSet = [NSCharacterSet decimalDigitCharacterSet];
//...    
[scanner scanCharactersFromSet:decimalCharSet
                    intoString:&temp1];
于 2013-08-08T22:57:02.853 回答