0

I have a string I want to display inside the uiwebview that may look like this:

"blah blah blah [IMG]you.png[/IMG] blah blah [IMG]me.png[/IMG]"

I want it replaced like this:

"blah blah blah <img src=\"you.png\"> blah blah <img src=\"me.png\">"

I can already get the string between the [IMG]...[/IMG] the problem is that it can only get the first set of [IMG]...[/IMG] and the rest of the occurrence cannot.

NSRange startRange = [finalQuestionString rangeOfString:@"[IMG]"];
if (startRange.location != NSNotFound) {
     NSRange targetRange;
     targetRange.location = startRange.location + startRange.length;
     targetRange.length = [finalQuestionString length] - targetRange.location;   
     NSRange endRange = [finalQuestionString rangeOfString:@"[/IMG]" options:0 
     range:targetRange];
     if (endRange.location != NSNotFound) {
         targetRange.length = endRange.location - targetRange.location;
         NSString *imageFile = [finalQuestionString 
         substringWithRange:targetRange];//the extracted filename
     }
}

so how can I get all the occurrences of [IMG]...[/IMG] and replace it with

"<img src=\"file.png\">"

any help would be appreciated. Thanks!

1 Answer 1

5

Since [IMG] will always become <img src=\" and [/IMG] will always become \">", why not do something like this:

NSString *originalString = @"blah blah blah [IMG]you.png[/IMG] blah blah [IMG]me.png[/IMG]";
NSString *tempString = [originalString stringByReplacingOccurrencesOfString:@"[IMG]" withString:@"<img src=\""];
NSString *finalString = [tempString stringByReplacingOccurrencesOfString:@"\"[/IMG]" withString:@"\'>'"];

Memory management is left as an exercise for the reader :)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! didn't thought it would be that simple :D I was focusing more on inserting a text in between. Thanks!

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.