如何去除字符串?

时间:2012-05-08 10:22:33

标签: objective-c nsstring substring

我有一个非常长的字符串,我只想在该字符串中提取某些字符串。我怎么能这样做?

例如我有:

this is the image <img src="http://vnexpress.net/Files/Subject/3b/bd/67/6f/chungkhoan-xanhdiem2.jpg"> and it is very beautiful.

现在是的,我希望获得这个长字符串的子字符串,只获得http://vnexpress.net/Files/Subject/3b/bd/67/6f/chungkhoan-xanhdiem2.jpg

请告诉我如何做到这一点。

3 个答案:

答案 0 :(得分:0)

您应该使用正则表达式,可能使用NSRegularExpression类。

这是一个完全符合您要求的示例(来自here):

- (NSString *)stripOutHttp:(NSString *)httpLine
{
    // Setup an NSError object to catch any failures
    NSError *error = NULL;  
    // create the NSRegularExpression object and initialize it with a pattern
    // the pattern will match any http or https url, with option case insensitive
    NSRegularExpression *regex = [NSRegularExpression
        regularExpressionWithPattern:@"https?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?" 
                             options:NSRegularExpressionCaseInsensitive
                               error:&error];
    // create an NSRange object using our regex object for the first match in the string httpline
    NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:httpLine
                                                         options:0
                                                           range:NSMakeRange(0, [httpLine length])];
    // check that our NSRange object is not equal to range of NSNotFound
    if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0)))
    {
        // Since we know that we found a match, get the substring from the parent
        // string by using our NSRange object
        NSString *substringForFirstMatch = [httpLine substringWithRange:rangeOfFirstMatch];
        NSLog(@"Extracted URL: %@",substringForFirstMatch);
        // return the matching string
        return substringForFirstMatch;
    }

    return NULL;
}

答案 1 :(得分:0)

NSString *urlString = nil;
NSString *htmlString = //Your string;

NSScanner *scanner = [NSScanner scannerWithString:htmlString];

[scanner scanUpToString:@"<img" intoString:nil];
if (![scanner isAtEnd]) {
    [scanner scanUpToString:@"http" intoString:nil];
    NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:@">"];
    [scanner scanUpToCharactersFromSet:charset intoString:&urlString];
}
NSLog(@"%@", urlString);

答案 2 :(得分:0)

您可以使用正则表达式:

NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:@"src=\"([^\"]*)\"" options:NSRegularExpressionCaseInsensitive error:nil];
NSString *text = @"this is the image <img src=\"http://vnexpress.net/Files/Subject/3b/bd/67/6f/chungkhoan-xanhdiem2.jpg\"> and it is very beautiful.";
NSArray *imgs = [regex matchesInString:text options:0 range:NSMakeRange(0, [text length])];
if (imgs.count != 0) {
    NSTextCheckingResult* r = [imgs objectAtIndex:0];
    NSLog(@"%@", [text substringWithRange:[r rangeAtIndex:1]]);
}

这个正则表达式是解决方案的核心:

src="([^"]*)"

它匹配src属性的内容,并捕获引号之间的内容(注意一对括号)。然后在[r rangeAtIndex:1]中检索此标题,并用于提取您要查找的字符串部分。