2

文字列内の文字列を置き換えたい。私の正確な目的が何であるかを説明させてください。

これは私の文字列です

<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 (textto speech) のためにそこにブレークを設定したいだけです。

4

1 に答える 1

5

それは機能します(すべて<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 |break|thisPTag|break|単語の途中または文末|break|を置き換えたい。

于 2012-04-26T16:17:42.603 に答える