Get nth character of a string in Swift programming language

后端 未结 30 2519
一整个雨季
一整个雨季 2020-11-22 01:26

How can I get the nth character of a string? I tried bracket([]) accessor with no luck.

var string = \"Hello, world!\"

var firstChar = string[         


        
30条回答
  •  孤城傲影
    2020-11-22 01:39

    You can do it by convert String into Array and get it by specific index using subscript as below

    var str = "Hello"
    let s = Array(str)[2]
    print(s)
    

提交回复
热议问题