Search code examples
iosobjective-cnsattributedstringnsrange

How the find the all the ranges of all strings between quotation marks


I have been working on this problem for a couple of days but can't find a solution to it. I have a very long string that contains a lot of quotes and I want to be able to find all of the strings (the ranges of this strings) so that I can bold them using NSMutableAttributedString

Example This is a string "with quoted" text and there is "some more" quoted text. I want to be able to turn this string into the following: This is a string "with quoted" text and there is "some more" quoted text.

This is what I have so far but it won't do the rest of the string:

    - (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];
              }
          }
      }

Solution

  • If text is too long this could be little bit slow however it works. Working on Regex solution.

    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 = @"";
        }
    }
    

    Edit

    Regex solution appears to be better/faster.

    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]);
    }