我想在uiwebview中显示一个字符串,如下所示:
"blah blah blah [IMG]you.png[/IMG] blah blah [IMG]me.png[/IMG]"
我希望它像这样取代:
"blah blah blah <img src=\"you.png\"> blah blah <img src=\"me.png\">"
我已经可以获得[IMG] ... [/ IMG]之间的字符串问题是它只能获得第一组[IMG] ... [/ IMG]而其余的事件不能
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
}
}
那么如何才能获得[IMG] ...... [/ IMG]的所有事件并将其替换为
"<img src=\"file.png\">"
任何帮助将不胜感激。谢谢!
答案 0 :(得分:5)
由于[IMG]
将永远变为<img src=\"
而[/IMG]
将永远变为\">"
,为什么不这样做:
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:@"\'>'"];
记忆管理留给读者练习:)