Mat*_*Car -1 string character swift
我需要检查一个单词是否以元音或辅音开头,如下所示:
let word = "ciao"
if wordStartsWithVowel {
print("Word starts with Vowel!")
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
extension Character {\n var isVowel: Bool { "aeiou".contains { String($0).compare(String(self).folding(options: .diacriticInsensitive, locale: nil), options: .caseInsensitive) == .orderedSame } }\n}\nRun Code Online (Sandbox Code Playgroud)\nextension StringProtocol {\n var startsWithVowel: Bool { first?.isVowel == true }\n}\nRun Code Online (Sandbox Code Playgroud)\n"ciao".startsWithVowel // false\n"Ciao".startsWithVowel // false\n"Artic".startsWithVowel // true\n"artic".startsWithVowel // true\n"\xc3\x81rtico".startsWithVowel // true\n"\xc3\xa1rtico".startsWithVowel // true\nRun Code Online (Sandbox Code Playgroud)\n