Swift:比较字符串的第一个字符

Swift: Compare the first character of string

如何将字符串的第一个字符与 Swift 中的字符进行比较?例如:

伪代码:

str = "my name is John"

if str[0] == m {
} 
let s = "abcd"

if s.hasPrefix("a") {  // takes a String or a literal
}

if s.first == "a" {  // takes a Character or a literal
}

if s[s.startIndex] == "a" { // takes a Character or a literal
}

这适用于 Swift 中的任何字符比较 3:

    var str: String = "abcd"    

    if str[str.index(str.startIndex, offsetBy: i)] == "a" {

    }

其中, i = 使用字符串的任何索引值 在上面的例子中它是 0 我 = 0