“Índice de string no Swift” Respostas de código

String de índice Swift

let string = "Hello, World!"
let index = string.index(string.startIndex, offsetBy: 3)

print(string[index]) // Prints "l"

// This can be done in one line too:
print(string[string.index(string.startIndex, offsetBy: 3)])
Passionate Amoeba

Índice de string no Swift

extension String {
    subscript(_ n: Int) -> Character {
        return self[self.index(self.startIndex, offsetBy: n)]
    }
}

let str = "Hello World"

print(str[0]) // H
print(str[4]) // o
print(str[6]) // w
Misty Mosquito

Respostas semelhantes a “Índice de string no Swift”

Perguntas semelhantes a “Índice de string no Swift”

Mais respostas relacionadas para “Índice de string no Swift” em Swift

Procure respostas de código populares por idioma

Procurar outros idiomas de código