Khant Thu Linn Khant Thu Linn - 11 months ago 120
iOS Question

Swift 2.0 String with substringWithRange

I am trying to get first char from String. It should be easy but I can't do in Swift 2.0 (with Xcode beta 6).

Get nth character of a string in Swift programming language

I have tried that method also. It use extension but I can't retrieve using that method. May I know how to do?


Two solutions without casting to NSString

let string = "Hello"
let firstChar1 = string.substringToIndex(string.startIndex.successor())

let firstChar2 = string.characters.first

Update for Swift 2:

Since Swift 2 returns Character rather than String a new String must be created.

let firstChar2 = String(string.characters.first!)