简体   繁体   English

如何从字符串 Swift 中删除 '\u{ef}' 字符

[英]How to remove '\u{ef}' character from String Swift

let's say I have a string假设我有一个字符串

var a = "#bb #cccc #ddddd\u{ef}" 

and i am setting it to textview like this我像这样将它设置为 textview

let text = a.trimmingCharacters(in: .whitespacesAndNewlines)
let textRemoved = text?.replacingOccurrences(of: "\u{ef}", with: "", options: NSString.CompareOptions.literal, range:nil)
textView.text = textRemove

I am trying to remove the \u{ef} character here.我想在这里删除\u{ef}字符。 But in textRemoved it is not happening.但是在textRemoved中它没有发生。 Please help me how to do it.请帮我怎么做。

I am using Xcode 10. Looks like below Xcode version than 10 is working fine.我正在使用 Xcode 10。看起来低于 Xcode 版本比 10 工作正常。 is it a bug of Xcode 10?它是 Xcode 10 的错误吗?

This is a late answer but I struggled to replace "\\u{ef}" in string as well.这是一个迟到的答案,但我也很难在字符串中替换 "\\u{ef}"。 During debugging when hovered over string it showed presence of \\u{ef} but when print in description it only showed space.在调试过程中,当鼠标悬停在字符串上时,它显示了 \\u{ef} 的存在,但在描述中打印时,它只显示了空格。

let str = "\u{ef} Some Title"
print(str) //" Some Title"

I tried replacingOccurrences(of: "\\u{ef}", with: "", options: NSString.CompareOptions.literal, range: nil).trimmingCharacters(in: .whitespaces) but it failed as well.我尝试replacingOccurrences(of: "\\u{ef}", with: "", options: NSString.CompareOptions.literal, range: nil).trimmingCharacters(in: .whitespaces)但它也失败了。

So I used below snippet and it worked like wonder.所以我使用了下面的代码片段,它就像奇迹一样工作。

 let modifiedStr = str.replacingOccurrences(of: "\u{fffc}", with: "", options: NSString.CompareOptions.literal, range: nil).trimmingCharacters(in: .whitespaces)
print(modifiedStr)  //"Some Title"

Hope this helps someone!!希望这对某人有帮助!!

i also faced same issue for "\\u{e2}" .我也面临同样的问题"\\u{e2}" i have searched a lot but unable to find any answer.我已经搜索了很多,但找不到任何答案。 then i have tried below code , which works for me.然后我尝试了下面的代码,这对我有用。

var newString = ""
for char in strMainString.unicodeScalars{
    if char.isASCII{
        newString += String(char)
    }
}

Hope that will also work for you too.希望这也适用于您。

In Xcode 10 Playground, string replaces for \\u{00EF} is working.在 Xcode 10 Playground 中, \\u{00EF}字符串替换正在工作。

var a = "#bb #cccc #ddddd\u{ef}" 
a = a.replacingOccurrences(of: "\u{00EF}", with: "")

I hope that will work for you.我希望这对你有用。

我尝试了以下方法,效果很好:

replacingOccurrences(of: "�", with: " ", options: NSString.CompareOptions.literal, range: nil)

eg 1例如 1

let text = "\u{ef}\u{ef}\u{ef}\u{ef}😇哦哦哦"
        
let text1 = text.replacingOccurrences(of: "\u{fffc}", with: "", options: String.CompareOptions.literal, range: nil)

let text2 = text.replacingOccurrences(of: "\u{ef}", with: "", options: String.CompareOptions.literal, range: nil).trimmingCharacters(in: .whitespaces)

runnable可运行的

 <img src="https://i.stack.imgur.com/styVo.png"/>

eg 2例如 2

let strBefore = textDocumentProxy.documentContextBeforeInput 
let strAfter  = textDocumentProxy.documentContextAfterInput 
var textInput = strBefore + strAfter

let textInput2 = textInput.replacingOccurrences(of: "\u{ef}", with: "", options: String.CompareOptions.literal, range: nil)
        
let textInput1 = textInput.replacingOccurrences(of: "\u{fffc}", with: "", options: String.CompareOptions.literal, range: nil).trimmingCharacters(in: .whitespaces)

runnable可运行的

 <img src="https://i.stack.imgur.com/xGHtW.png"/>

Similar to question but with \\u{e2} symbol (fix is the same):与问题类似,但带有\\u{e2}符号(修复是相同的):

\\u{e2} is not a character rather subset of UTF8 plane which starts with 0xE2 byte. \\u{e2}不是字符,而是以0xE2字节开头的 UTF8 平面的子集。

So look here , E2 are general punctuation symbols.所以看这里,E2是一般标点符号。 There many symbols actually which started with \\u{e2} but not limited to it and full char can be represented fe with e2 80 a8 bytes (line separator).实际上有许多以\\u{e2}开头的符号,但不限\\u{e2} ,完整的字符可以用e2 80 a8字节(行分隔符)表示。

That explains why shown in Xcode \\u{e2} can't be replaced with replacingOccurrences ... function.这解释了为什么 Xcode \\u{e2}中显示的内容不能替换为replacingOccurrences ... 函数。 In order to filter out correct symbol you have to know what exact symbol it is, fe by using the snippet below:为了过滤掉正确的符号,您必须使用以下代码段知道它是什么确切的符号:

"\u{2028}&😲".forEach { (char) in
   print(Data(char.utf8).map { String(format: "%02x", $0) }.joined(separator: " "))
 }

it prints to console:它打印到控制台:

e2 80 a8
26
f0 9f 98 b2

which are byte representation for each symbol.这是每个符号的字节表示。

Next step is to filter your string, go here and search in 3d column your bytes and unicode code point value is what you need (first column) and write it in swift code like "\\u{2028}\\u{206A}..." (depending on your sorting).下一步是过滤你的字符串,到这里在 3d 列中搜索你的字节和 unicode 代码点值是你需要的(第一列),然后用像"\\u{2028}\\u{206A}..." (取决于您的排序)。

The final function may look like:最终的函数可能如下所示:

func removingE2Symbols() -> String {
    let specialChars = "\u{202A}\u{202C}"
    return filter { !specialChars.contains($0) }
}

Try this尝试这个

extension String {
    var asciiString: String {
        return String(self.unicodeScalars.filter{ $0.isASCII })
    }   
}

It,s working Please check again:它正在工作请再次检查:

 let a = "#bb #cccc #ddddd\u{ef}"
 let text = a.trimmingCharacters(in: .whitespacesAndNewlines)
 let textRemoved = text.replacingOccurrences(of: "\u{ef}", with: "", options: NSString.CompareOptions.literal, range:nil)
 print(textRemoved)

在此处输入图片说明

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

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