Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 289
Istiak Morsalin Avatar asked Oct 19 '18 05:10

Istiak Morsalin


People also ask

How do I remove a specific character from a string in Swift?

In the Swift string, we check the removal of a character from the string. To do this task we use the remove() function. This function is used to remove a character from the string. It will return a character that was removed from the given string.

How do I remove a word from a string in Swift?

Swift String remove() The remove() method removes a character in the string at the specified position.

How do you remove the last character of a string in Swift?

Swift String dropLast() The dropLast() method removes the last character of the string.


1 Answers

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

like image 106
Rahul Serodia Avatar answered Nov 17 '22 16:11

Rahul Serodia