所以我试图把你的第一个字符串索引(fullName)并测试它以查看它是否匹配所有元音低和大写......出于某种原因,当我使用.startIndex我一次测试一个字母用if语句。
我有办法一次测试所有元音()我试过||在每个之间它给了我错误"无法将字符串转换为Bool"。感谢任何帮助。
func lyricsForName(lyricsTemplate: String, fullName: String) -> String {
let shortName = shortNameFromName(name: fullName)
let index = fullName[fullName.startIndex]
if index== ("a","A"){
let lyrics = lyricsTemplate
.replacingOccurrences(of:"<FULL_NAME>", with: fullName)
.replacingOccurrences (of:"<SHORT_NAME>", with: fullName )
return lyrics
}else{
let lyrics = lyricsTemplate
.replacingOccurrences(of:"<FULL_NAME>", with: fullName)
.replacingOccurrences (of:"<SHORT_NAME>", with: shortName )
return lyrics
答案 0 :(得分:1)
这是一种方法。您可以使用元音数组,然后测试数组是否包含第一个字符:
let fullName = "Albert"
let vowels = "aeiouAEIOU".characters
if let first = fullName.characters.first, vowels.contains(first) {
print("\(fullName) starts with a vowel")
}
Albert以元音开头
注意:使用fullName.characters.first
比fullName[fullName.startIndex]
更安全,因为后者会因空String
而崩溃。
答案 1 :(得分:1)
您可以像之前的答案那样做,但这是一种使用可用API的更快捷方式:
extension String {
var firstThreeLettersAreVowels: Bool {
guard characters.count >= 3 else {
return false
}
let firstThreeLetters = substring(to: index(startIndex, offsetBy: 3))
let isAllVowels = CharacterSet(charactersIn: firstThreeLetters).isSubset(of: CharacterSet.vowels)
return isAllVowels
}
var isLower: Bool {
return CharacterSet(charactersIn: self).isSubset(of: CharacterSet.lowercaseLetters)
}
var isUpper: Bool {
return CharacterSet(charactersIn: self).isDisjoint(with: CharacterSet.lowercaseLetters)
}
}
extension CharacterSet {
static var vowels: CharacterSet {
return CharacterSet(charactersIn: "AEIOUYaeiouy")
}
}
或者一个班轮(没有长度检查)是
extension String {
var firstThreeLettersAreVowels: Bool {
return CharacterSet(charactersIn: substring(to: index(startIndex, offsetBy: 3)))
.isSubset(of: CharacterSet(charactersIn: "AEIOUYaeiouy"))
}
}
这允许您依赖现有的API,而不是自己尝试工作,而且应该非常快。
答案 2 :(得分:0)
所有荣誉请到PEEJWEEJ回答
字符集是他的想法:)
我只是更通用了
import Foundation
extension String {
func isFirstLetters(count: Int, of characterSet: CharacterSet) -> Bool {
guard characters.count >= count else {
return false
}
let firstLetters = substring(to: index(startIndex, offsetBy: count))
let isInSubset = CharacterSet(charactersIn: firstLetters).isSubset(of: characterSet
)
return isInSubset
}
}
extension CharacterSet {
static var vowels: CharacterSet {
return CharacterSet(charactersIn: "AEIOUYaeiouy")
}
}
一些测试用例:
"abcDEf".isFirstLetters(count: 3, of: .vowels) // false
"aioDEf".isFirstLetters(count: 3, of: .vowels) // true
"abcDEf".isFirstLetters(count: 1, of: .vowels) // true
"bbcDEf".isFirstLetters(count: 1, of: .vowels) // false
"ibcDEf".isFirstLetters(count: 1, of: .vowels) // true
"ABcdef".isFirstLetters(count: 3, of: .uppercaseLetters) // false
"ABDdef".isFirstLetters(count: 3, of: .uppercaseLetters) // true
"abBABcdef".isFirstLetters(count: 3, of: .lowercaseLetters) // false
"abbABDdef".isFirstLetters(count: 3, of: .lowercaseLetters) // true
"ABD".isFirstLetters(count: 3, of: CharacterSet(charactersIn: "A"..."C")) // false
"ABD".isFirstLetters(count: 3, of: CharacterSet(charactersIn: "A"..."D")) // true
调试信息:
extension CharacterSet {
var characters: [Character] {
var result: [Character] = []
for plane: UInt8 in 0...16 where self.hasMember(inPlane: plane) {
for unicode in UInt32(plane) << 16 ..< UInt32(plane + 1) << 16 {
if let uniChar = UnicodeScalar(unicode), self.contains(uniChar) {
result.append(Character(uniChar))
}
}
}
return result
}
}
print(CharacterSet.uppercaseLetters.description)
//<Foundation._SwiftNSCharacterSet: 0x6000000271a0>
print(String(CharacterSet.vowels.characters))
//AEIOUYaeiouy
print(CharacterSet.vowels.characters)
// ["A", "E", "I", "O", "U", "Y", "a", "e", "i", "o", "u", "y"]