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]"
开发者_JS百科
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!
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 :)
精彩评论