簡體   English   中英

從字符串中刪除U \\ 0000fffc unicode標量

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

我收到NSAttributedString包含NSTextAttachment 我想刪除該附件,看起來它在字符串中表示為"\\u{ef}" 打印這種字符串的unicode標量,似乎"\\u{ef}" unicode標量是U\\0000fffc

我試着這樣做:

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

沒有成功,所以我正在嘗試比較unicode標量:

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

但是我無法在比較中取得成功。 我怎么能這樣做?

但是這個代碼對我來說如何從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)

輸出:

“第一部分 - 最后一部分”

第一部分最后一部分

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