0

I found a lot of examples how to find string between 2 strings, but none of which shows how to handle multiple occurrences of that string. I have for example string like this

"Hi, I am <id>User</id>. I am 20 <id>years old</id>, and live in <id>some country</id>."

The idea behind is that I want to hyperlink each occurrence of that string within UITextField, and remove tags from the string. I also have 2 types of the tag, one should display hyperlink, the other should popup alert view with some text description of the word or phrase clicked.

EDIT:

Found a perfectly good working solution to extend this logic with changing content of the text with attributed string between tags provided in the text. Link here.

2
  • Is the string constant or will the text vary? Commented Nov 8, 2017 at 9:25
  • Server side generates the string, and I get a list of this strings. Around 100 items displayed with hyperlinks. Commented Nov 8, 2017 at 9:28

2 Answers 2

1

@Aleksandar Try this.. it works for me..

NSString *serverOutput = @"Hi, I am <id>User</id>. I am 20 <id>years old</id>, and live in <id>some country</id>.";
    if([serverOutput containsString:@"</id>"])
    {
        NSArray *arrSeparate = [serverOutput componentsSeparatedByString:@"</id>"];
        NSString *output = @"";
        for(int i=0; i<arrSeparate.count; i++)
        {
            if([[arrSeparate objectAtIndex:i] containsString:@"<id>"])
            {
                NSArray *arrTest = [[arrSeparate objectAtIndex:i] componentsSeparatedByString:@"<id>"];
                if(output.length < 1)
                    output = [arrTest objectAtIndex:1];
                else
                    output = [NSString stringWithFormat:@"%@\n%@",output,[arrTest objectAtIndex:1]];
            }
        }
        serverOutput = output;
    }
    NSLog(@"%@", serverOutput);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this works for me as well. I can work from there to achieve what I need. Both you and @gurmandeep gave a working solution.
1

Please look into this, and i hope this gets you all the range where the keyword exists

NSString *serverOutput = @"Hi, I am <id>User</id>. I am 20 <id>years old</id>, and live in <id>some country</id>";
NSUInteger count = 0, length = [serverOutput length];
NSRange startRange = NSMakeRange(0, length);
NSRange endRange = NSMakeRange(0, length);
while(startRange.location != NSNotFound)
{
    startRange = [serverOutput rangeOfString: @"<id>" options:0 range:startRange];
    if(startRange.location != NSNotFound)
    {
        endRange = [serverOutput rangeOfString: @"</id>" options:0 range:endRange];
        startRange = NSMakeRange(startRange.location + startRange.length, length - (startRange.location + startRange.length));
        endRange = NSMakeRange(endRange.location + endRange.length, length - (endRange.location + endRange.length));
        count++;
    }
}

startRange will be the range from where the tag starts and endRange is where starts

You can change the range, location, create attributed string and add hyperlink as the range of string is available to you.

1 Comment

Happy coding Mate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.