Get nth character of a string in Swift programming language

后端 未结 30 1957
一整个雨季
一整个雨季 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 01:38

    By now, subscript(_:) is unavailable. As well as we can't do this

    str[0] 
    

    with string.We have to provide "String.Index" But, how can we give our own index number in this way, instead we can use,

    string[str.index(str.startIndex, offsetBy: 0)]
    

提交回复
热议问题