2

I can not find documentation on Apple's developer site. I have also been searching for the past few hours without a well-explained answer. I am trying to learn swift currently and I am trying to access a nth char in a string. In c++ I know you can do this with str[int value] ie.

string str = "This is a test string"; cout << str[2] << endl;

should print out "i" if I counted right. I am trying to do/find a way to do this in Swift and have not found an easy or way to do this. Is anyone able to help give some input?

Helgrind
  • 23
  • 1
  • 4

1 Answers1

7

Its pretty simple :

    let myText = Array("Hello World !!!".characters)
    print(myText[2]) // "l" will be printed
ankit
  • 3,537
  • 1
  • 16
  • 32
  • Thank you, I thought converting it to a char array might be the only way. I just wanted to make sure. Since C++ you can just access the String as if it is an array since it is. – Helgrind Apr 29 '17 at 06:27