问题描述
我想在 uiwebview 中显示一个字符串,它可能如下所示:
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]"
我想把它替换成这样:
"blah blah blah <img src=\"you.png\"> blah blah <img src=\"me.png\">"
我已经可以获取 [IMG]...[/IMG] 之间的字符串了,问题是它只能获取第一组 [IMG]...[/IMG] 而其余的出现不能.
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
}
}
那么我怎样才能获得所有出现的 [IMG]...[/IMG] 并将其替换为
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!
推荐答案
由于 [IMG]
将永远变成 <img src=\"
和 [/IMG]
总是会变成 \">"
,为什么不这样做:
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 :)
这篇关于替换出现之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!