Quantcast
Viewing all articles
Browse latest Browse all 8

Answer by Leo Dabus for How to remove special characters from string in Swift 2?

If you need to keep only ascii printable characters in a string you can just filter them checking if their unicodeScalar values are ascii:

extension Character {
    var isAscii: Bool {
        return unicodeScalars.allSatisfy { $0.isASCII }
    }
}

extension RangeReplaceableCollection where Self: StringProtocol {
    var asciiPrintable: Self {
        return filter { $0.isAscii }
    }
}

let string = "cafe\u{301}"
let asciiPrintable = string.asciiPrintable
print(asciiPrintable)  // "caf"

Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>