2014-07-15 64 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

相關問題