-1

I need to filter invisible character from a string. In the attached screen shot, string "​Cilantro" has some hidden character and when i get count of this before and after removing hidden character it shows different character count.

I just want to filter invisible characters not special characters, for example I dont want to filter á, ã è etc characters.

Note: I removed hidden characters using "delete" button.

For the reference I am adding here the String with invisible character: "​Cilantro". I am not sure if it will show at your end too.

enter image description here

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
CMA
  • 1,528
  • 1
  • 11
  • 22

1 Answers1

4

Swift 5 or later

You can use new Character isLetter property

let del = Character(UnicodeScalar(127)!)
let string = "Cilantro\(del)\(del)"
print(string.count) // "10\n"
let filtered = string.filter { $0.isLetter }
print(filtered.count)  // "8\n"

let string = "cafe\u{301}"
let filtered = string.filter { $0.isLetter }
print(filtered)  // "café"

If you just want to remove zero width spaces from your string you can do as follow:


extension Character {
    static let zeroWidthSpace = Self(.init(0x200B)!)
    var isZeroWidthSpace: Bool { self == .zeroWidthSpace }
}

extension Bool {
    var negated: Bool { !self }
}

let str = "​Cilantro"
print(str.count) // 9

let filtered = str.filter(\.isZeroWidthSpace.negated)
print(filtered.count) // 8
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 4
    As of Swift 5 you can simply do `string.filter { $0.isASCII }` – Martin R Mar 04 '19 at 14:18
  • 2
    It is part of https://github.com/apple/swift-evolution/blob/master/proposals/0221-character-properties.md. – Martin R Mar 04 '19 at 14:21
  • @MartinR nice there is a lot of new stuff coming up next release. – Leo Dabus Mar 04 '19 at 14:22
  • 1
    Actually there is a problem with your implementation, it relies on the “precomposed” representation of characters. Try it with `let string = "cafe\u{301}"` – Martin R Mar 04 '19 at 14:30
  • @LeoDabus Thanks for answering my question, I just want to remove invisible characters. For example i do want some special characters like "è, á", and above method will not allow these characters. – CMA Mar 05 '19 at 06:55
  • Well you need to define which characters are valid – Leo Dabus Mar 05 '19 at 12:59
  • 1
    @CMA edited to filter only letters. If you want to allow digits, white spaces and newlines you just need to add it to the logic `{ $0.isLetter || $0.isNumber }` – Leo Dabus Jun 14 '19 at 12:19