Answers for "work with string index in swift"

1

index of string in array swift

let arr = ["a","b","c","a"]

let indexOfA = arr.firstIndex(of: "a") // 0
let indexOfB = arr.lastIndex(of: "a") // 3
Posted by: Guest on January-30-2020
0

string index in 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
Posted by: Guest on January-09-2022

Code answers related to "Swift"

Browse Popular Code Answers by Language