Get nth character of a string in Swift programming language

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

    In Swift 5 without extension to the String :

    var str = "ABCDEFGH"
    for char in str {
    if(char == "C") { }
    }
    

    Above Swift code as same as that Java code :

    int n = 8;
    var str = "ABCDEFGH"
    for (int i=0; i

提交回复
热议问题