Swift String.utf16.index(quot:offsetBy:)不';无法在包含表情符号的字符串上按预期工作

Swift String.utf16.index(quot:offsetBy:)不';无法在包含表情符号的字符串上按预期工作,swift,string,unicode,emoji,utf-16,Swift,String,Unicode,Emoji,Utf 16,我正在编写一个代码,用string3替换string2中所有出现的string1。例如: string1: "he" string2: "hello everybody. I'm here to help you." string3: "XY" output: "XYllo everybody. I'm XYre to XYlp you." 一路上,我注意到如果string1或string2包含表情符号,我的方法就不能正常工作。我写了一小段代码来说明问题: let strA=“你好,我的朋友

我正在编写一个代码,用string3替换string2中所有出现的string1。例如:

string1: "he"
string2: "hello everybody. I'm here to help you."
string3: "XY"
output:  "XYllo everybody. I'm XYre to XYlp you."
一路上,我注意到如果string1或string2包含表情符号,我的方法就不能正常工作。我写了一小段代码来说明问题:

let strA=“你好,我的朋友”
让strB=“嗨,我的朋友”

设strC=“这可能有助于隐式编码假设,即当您在屏幕上看到UTF16代码点和字符时,它们之间存在一对一的关联。正如你所发现的,那不是真的
通常,您必须仅对创建索引时使用的集合使用索引
idx1
是指向
strA.utf16
的索引,不能与
strB.utf16
strC.utf16
一起使用。