gpt4 book ai didi

iphone - 替换出现之间的字符串

转载 作者:行者123 更新时间:2023-11-28 18:43:50 25 4
gpt4 key购买 nike

我想在 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\">"

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

[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:@"\'>'"];

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

关于iphone - 替换出现之间的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7564725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com