简体   繁体   中英

Remove U\0000fffc unicode scalar from string

I receive an NSAttributedString that contains a NSTextAttachment . I want to remove that attachment, and it looks like it is represented as "\\u{ef}" in the string. Printing the unicode scalars of such string, it also seems that unicode scalar for the "\\u{ef}" is 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:

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?

    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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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