替换出现之间的字符串
我想在 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\">"
任何帮助替换它,我们将不胜感激。谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
[IMG]
将始终变为且
[/IMG]
将始终变为\"> “
,为什么不做这样的事情:内存管理留给读者作为练习:)
Since
[IMG]
will always become<img src=\"
and[/IMG]
will always become\">"
, why not do something like this:Memory management is left as an exercise for the reader :)