How to remove all non-ASCII characters from a string in Swift

1 Answer

0 votes
import Foundation

func removeNonASCII(_ input: String) -> String {
    return input.filter { $0.asciiValue ?? 128 <= 127 }
}

let input = "©€ABC£µ¥xyz!® 123 こんにちは"
let filtered = removeNonASCII(input)

print("Filtered string: \(filtered)")



/*
run:

Filtered string: ABCxyz! 123  

*/

 



answered Jun 13, 2025 by avibootz
...