gpt4 book ai didi

objective-c - 如何替换字符串中的部分字符串?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:32:35 24 4
gpt4 key购买 nike

我想替换字符串中的字符串。让我向您解释一下我的确切目标是什么。

这是我的字符串

<p>Hello Webseite i want to replace<p>thisPTag</p>which is in the middle 
of the word or in the end of the sentence</p>.

如果我用

[string stringByReplacingOccurrencesOfString:@"<p>" withString:@"|break|"]; 

它仅替换第一个

。但是我想替换所有p标签,无论是否在句子中,前后的空格也无关紧要。
那我该怎么做呢?

不是很重要,但很高兴知道:
请记住,我不想剥离 html,我只是想替换 p-tag 以在那里设置中断以实现正确的 SSML 和 TTS(文本到语音)

最佳答案

有效(所有 <p> 都被替换)。你可能只是混淆了 <p></p> .这些是不同的文字,因此必须分别替换它们:

NSString * a = @"<p>Hello Webseite i want to replace<p>thisPTag</p>which is in the middle of the word or in the end of the sentence</p>. ";

NSString * b = [a stringByReplacingOccurrencesOfString:@"<p>" withString:@"|break|"];
b = [b stringByReplacingOccurrencesOfString:@"</p>" withString:@"|break|"];

NSLog(@"%@",b);

这打印:

|break|Hello Webseite i want to replace|break|thisPTag|break|which is in the middle of the word or in the end of the sentence|break|.

关于objective-c - 如何替换字符串中的部分字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10337083/

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