简体   繁体   English

从字符串中删除U \\ 0000fffc unicode标量

[英]Remove U\0000fffc unicode scalar from string

I receive an NSAttributedString that contains a NSTextAttachment . 我收到NSAttributedString包含NSTextAttachment I want to remove that attachment, and it looks like it is represented as "\\u{ef}" in the string. 我想删除该附件,看起来它在字符串中表示为"\\u{ef}" Printing the unicode scalars of such string, it also seems that unicode scalar for the "\\u{ef}" is U\\0000fffc . 打印这种字符串的unicode标量,似乎"\\u{ef}" unicode标量是U\\0000fffc

I tried to do this: 我试着这样做:

noAttachmentsText = text.replacingOccurrences(of: "\u{ef}", with: "")

with no success, so I'm trying by comparing unicode scalars: 没有成功,所以我正在尝试比较unicode标量:

var scalars = Array(text.unicodeScalars)
for scalar in scalars {
   // compare `scalar` to `U\0000fffc`
}

but I'm not able either to succeed in the comparison. 但是我无法在比较中取得成功。 How could I do this? 我怎么能这样做?

But this code works for me from How do I remove "\\U0000fffc" from a string in Swift? 但是这个代码对我来说如何从Swift中的字符串中删除“\\ U0000fffc”?

    let original = "First part \u{ef} Last part"
    let originalRange = Range<String.Index>(start: original.startIndex, end: original.endIndex)
    let target = original.stringByReplacingOccurrencesOfString("\u{ef}", withString: "", options: NSStringCompareOptions.LiteralSearch, range: originalRange)

    print(target)

Output : 输出:

"First part ï Last part" “第一部分 - 最后一部分”

to

First part Last part 第一部分最后一部分

U can use similar code for swift 3 just replace unicode using replacingOccurrences option for exapmle : U可以使用类似的代码迅速3.采用replacingOccurrences选项exapmle只需更换的Unicode:

func stringTocleanup(str: String) -> String {
        var result = str
        result = result.replacingOccurrences(of: "\"", with: "\"")
            .replacingOccurrences(of: "\u{10}", with: "")
        return result
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM