You can strip punctuation from the user’s guess using a CharacterSet.
let str = "HELLO!"
let cleanStr = str.trimmingCharacters(in: .punctuationCharacters)
//parameter is a CharacterSet
print(cleanStr)
//prints HELLO
Note that CharacterSet.punctuationCharacters covers far more than just ? and ! By my count, Unicode General Category P* contains nearly 800 characters!
when doing voice to voice matching — I think it would be a great idea - no? I am afraid that the system is not going to recognize someone using exclamatory or question in their voice…is stripping the punctuation via CharacterSet the best way you would do it??
I thought when using an input like Siri with a textBox, it’ll only put an exclamation mark when someone literally says “exclamation mark” while talking
I mean like if the correct string is “what is your name?” but when the user speaks “what is your name” if the phone does not recognize the “?” it will say it is wrong - so i would just like to remove that possibility from happening!!
Informally, this set is the set of all non-whitespace characters used to separate linguistic units in scripts, such as periods, dashes, parentheses, and so on.
Unfortunately, the code I posted above only strips punctuation from the beginning and end of a string. Since commas usually appear internally, they won’t get removed.
You can use this instead:
import Foundation
let s = "Hello, my name is Simon!"
print(s.trimmingCharacters(in: .punctuationCharacters))
//Hello, my name is Simon
extension String {
var withoutPunctuation: String {
self.components(separatedBy: .punctuationCharacters).joined(separator: "")
}
}
print(s.withoutPunctuation)
//Hello my name is Simon