Get nth character of a string in Swift programming language

后端 未结 30 1971
一整个雨季
一整个雨季 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:52

    My solution is in one line, supposing cadena is the string and 4 is the nth position that you want:

    let character = cadena[advance(cadena.startIndex, 4)]
    

    Simple... I suppose Swift will include more things about substrings in future versions.

提交回复
热议问题