本文介绍了检查单词是否以元音或辅音开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查单词是否以元音或辅音开头,诸如此类:

I need to check if a word starts with a vowel or a consonant, something like that:

let word = "ciao"

if wordStartsWithVowel {
    print("Word starts with Vowel!")
}

我该怎么做?

推荐答案

let vowels: [Character] = ["a","e","i","o","u"]
let word = "ciao"

if vowels.contains(word.lowercased().characters.first!) {
    print("Word starts with Vowel!")
}

.lowercaseString 很重要,因为否则,大写元音不会被识别为元音。

.lowercaseString is important because, otherwise, uppercase vowels wouldn't be recognised as vowels.

这篇关于检查单词是否以元音或辅音开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:41