2014-07-15 34 views
1

我一直在解决这个问题几天,但无法找到解决方案。我有一个包含大量的报价很长的字符串,我希望能够找到所有的字符串(此字符串的范围),这样我可以大胆他们使用NSMutableAttributedString如何查找引号之间的所有字符串的所有范围

例 这是一个字符串“引用“的文字,还有一些”引用的文字“。 我希望能够将此字符串转换为以下内容: 这是一个字符串“带有引用的”文本并且有“一些更多”引用的文本。

这是我迄今为止,但它不会做字符串的其余部分:

- (void)stringBetweenString:(NSString*)start andString:(NSString*)end inString:(NSMutableAttributedString *)text 
    { 
      NSRange startRange = [[text string] rangeOfString:start]; 
      if (startRange.location != NSNotFound) 
      { 
       NSRange targetRange; 
       targetRange.location = startRange.location + startRange.length; 
       targetRange.length = [text length] - targetRange.location; 
       NSRange endRange = [[text string] rangeOfString:end options:0 range:targetRange]; 
       if (endRange.location != NSNotFound) 
       { 
        targetRange.length = endRange.location - targetRange.location; 
       [text addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Helvetica-Bold" size:12.0] range:targetRange]; 
       } 
      } 
     } 
+0

您是否听说过NSRegularExpression? – matt

回答

1

如果文本太长,这可能是有点慢但它的工作原理。使用正则表达式解决方案。

NSString *string = @"Example This is a string \"with quoted1\" text and there is \"some more1\" quoted text. I want to be able to turn this string into the following: This is a string \"with quoted2\" text and there is \"some more2\" quoted text."; 

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string attributes:nil]; 

int leftFromLeft = 0; 

while ([string rangeOfString:@"\""].location != NSNotFound) { 

    NSRange quoteLocationFirst = [string 
            rangeOfString:@"\"" 
            options:0 
            range:NSMakeRange(leftFromLeft, string.length - leftFromLeft) 
            ]; 

    leftFromLeft = quoteLocationFirst.location + quoteLocationFirst.length; 

    NSRange quoteLocationSecond = [string 
            rangeOfString:@"\"" 
            options:0 
            range:NSMakeRange(leftFromLeft, string.length - leftFromLeft) 
            ]; 

    NSRange quotedTextRange = NSMakeRange(
            quoteLocationFirst.location, 
            quoteLocationSecond.location - quoteLocationFirst.location + 1 
           ); 

    UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:30.0f]; 
    [attString addAttribute:NSFontAttributeName value:font range:quotedTextRange]; 

    NSLog(@"%@ \r\n\r\n", [string substringWithRange:quotedTextRange]); 

    leftFromLeft = quoteLocationSecond.location + quoteLocationSecond.length; 

    if ([string rangeOfString:@"\"" options:0 range:NSMakeRange(leftFromLeft, string.length - leftFromLeft)].location == NSNotFound) { 
     string = @""; 
    } 
} 

编辑

正则表达式的解决方案似乎是更好/更快。

NSString *string = @"Example This is a string \"with quoted1\" text and there is \"some more1\" quoted text. I want to be able to turn this string into the following: This is a string \"with quoted2\" text and there is \"some more2\" quoted text. Example This is a string \"with quoted3\" text and there is \"some more3\" quoted text. I want to be able to turn this string into the following: This is a string \"with quoted4\" text and there is \"some more4\" quoted text."; 

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string attributes:nil]; 

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\"([^\"]*)\"" options:NSRegularExpressionCaseInsensitive error:nil]; 

NSArray *arrayOfAllMatches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)]; 

for (NSTextCheckingResult *match in arrayOfAllMatches) { 

    UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:30.0f]; 
    [attString addAttribute:NSFontAttributeName value:font range:match.range]; 

    //NSLog(@"%@", [string substringWithRange:match.range]); 
} 
+0

你是男人!正则表达式确实工作了很多,非常感谢! – RockPaperScissors

相关问题