How to remove '\u{ef}' character from String Swift
Asked Answered
C

8

6

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?

Chang answered 19/10, 2018 at 5:26 Comment(8)
So does the text contain the 6 characters \u{ef}, or the code point U+00EF LATIN SMALL LETTER I WITH DIAERESIS?Saldivar
This code works correctly in playgroundBamberg
Try doing text?.replacingOccurrences(of: "\\u{ef}"... with double backslash.Saldivar
The code is working fine is PlaygroundTengdin
for char in strMainString.unicodeScalars{ if char.isASCII{ newNumber += String(char) } }Derryberry
Check this thread of Apple's dev forums. Do you think it is related to your issue?Totem
@Totem I think my issue is related to the one you mentionedChang
@JasonBourne, the thread I have shown tells us two thing: -- Do not rely on the outputs in the debug area of Xcode. -- The extra character you need remove may not be \{ef}. Try removing \u{FFFC} instead, or check what is the actual extra character by putting print(text.data(using: .utf8) as! NSData).Totem
P
7

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

Photon answered 1/7, 2019 at 10:50 Comment(0)
D
0

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.

Derryberry answered 19/10, 2018 at 6:20 Comment(1)
This solution returned empty string in every case in arabic search it will not only remove \u{e2}"Benumb
O
0

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.

Ortego answered 19/10, 2018 at 10:41 Comment(1)
its not working. trying to get a text from textview.Chang
G
0

I tried the following and it worked like a charm:

replacingOccurrences(of: "�", with: " ", options: NSString.CompareOptions.literal, range: nil)
Gal answered 19/3, 2019 at 9:2 Comment(0)
C
0

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.sstatic.net/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.sstatic.net/xGHtW.png"/>
Cotillion answered 27/8, 2020 at 7:57 Comment(0)
D
0

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) }
}
Domination answered 6/10, 2020 at 12:28 Comment(0)
N
0

Try this

extension String {
    var asciiString: String {
        return String(self.unicodeScalars.filter{ $0.isASCII })
    }   
}
Northeasterly answered 20/4, 2022 at 19:43 Comment(0)
K
-2

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

Koine answered 19/10, 2018 at 5:36 Comment(2)
it might possible that it is not working , as it is special character. I also faced same issue for "\\u{e2}"Derryberry
I can not figure out what is wrong with my thought process, I tried everything but can not eliminate the unicode character when i am trying to get text from the textfield :(Chang

© 2022 - 2024 — McMap. All rights reserved.