替换出现之间的字符串

发布于 2024-12-06 10:11:55 字数 1116 浏览 0 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左秋 2024-12-13 10:11:55

由于 [IMG] 将始终变为 [/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:@"\'>'"];

内存管理留给读者作为练习:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文