【问题标题】:How to strip down the string?如何剥离字符串?
【发布时间】:2012-05-08 10:22:33
【问题描述】:

我有一个很长的字符串,我只想在该字符串中提取一些特定的字符串。我该怎么做?

例如我有:

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

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

【问题讨论】:

    标签: objective-c nsstring substring


    【解决方案1】:

    您可以为此使用正则表达式:

    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] 中检索此标题,并用于提取您要查找的字符串部分。

    【讨论】:

      【解决方案2】:

      您应该使用正则表达式,可能使用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;
      }
      

      【讨论】:

        【解决方案3】:
        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);
        

        【讨论】:

          猜你喜欢
          • 2021-08-12
          • 2010-09-19
          • 2012-09-30
          • 1970-01-01
          • 1970-01-01
          • 2018-03-16
          • 1970-01-01
          • 2018-04-20
          相关资源
          最近更新 更多