'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

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. But in textRemoved it is not happening. Please help me how to do it.

I am using Xcode 10. Looks like below Xcode version than 10 is working fine. is it a bug of Xcode 10?



Solution 1:[1]

This is a late answer but I struggled to replace "\u{ef}" in string as well. During debugging when hovered over string it showed presence of \u{ef} but when print in description it only showed space.

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.

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!!

Solution 2:[2]

i also faced same issue for "\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.

Solution 3:[3]

In Xcode 10 Playground, string replaces for \u{00EF} is working.

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

I hope that will work for you.

Solution 4:[4]

I tried the following and it worked like a charm:

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

Solution 5:[5]

e.g. 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"/>

e.g. 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"/>

Solution 6:[6]

Similar to question but with \u{e2} symbol (fix is the same):

\u{e2} is not a character rather subset of UTF8 plane which starts with 0xE2 byte.

So look here, E2 are general punctuation symbols. There many symbols actually which started with \u{e2} but not limited to it and full char can be represented f.e. with e2 80 a8 bytes (line separator).

That explains why shown in Xcode \u{e2} can't be replaced with replacingOccurrences... function. In order to filter out correct symbol you have to know what exact symbol it is, f.e. 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).

The final function may look like:

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

Solution 7:[7]

Try this

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

Solution 8:[8]

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)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rahul Serodia
Solution 2 Saurabh Prajapati
Solution 3 Emre Ă–zdil
Solution 4 98chimp
Solution 5
Solution 6
Solution 7 Arun Srithar
Solution 8 Jogendar Choudhary