How can I get the nth character of a string? I tried bracket([]
) accessor with no luck.
var string = \"Hello, world!\"
var firstChar = string[
My very simple solution:
Swift 4.1:
let myString = "Test string"
let index = 0
let firstCharacter = myString[String.Index(encodedOffset: index)]
Swift 5.1:
let firstCharacter = myString[String.Index.init(utf16Offset: index, in: myString)]
No indexing using integers, only using String.Index
. Mostly with linear complexity. You can also create ranges from String.Index
and get substrings using them.
Swift 3.0
let firstChar = someString[someString.startIndex]
let lastChar = someString[someString.index(before: someString.endIndex)]
let charAtIndex = someString[someString.index(someString.startIndex, offsetBy: 10)]
let range = someString.startIndex..<someString.index(someString.startIndex, offsetBy: 10)
let substring = someString[range]
Swift 2.x
let firstChar = someString[someString.startIndex]
let lastChar = someString[someString.endIndex.predecessor()]
let charAtIndex = someString[someString.startIndex.advanceBy(10)]
let range = someString.startIndex..<someString.startIndex.advanceBy(10)
let subtring = someString[range]
Note that you can't ever use an index (or range) created from one string to another string
let index10 = someString.startIndex.advanceBy(10)
//will compile
//sometimes it will work but sometimes it will crash or result in undefined behaviour
let charFromAnotherString = anotherString[index10]
let str = "My String"
String at index
let index = str.index(str.startIndex, offsetBy: 3)
String(str[index]) // "S"
Substring
let startIndex = str.index(str.startIndex, offsetBy: 3)
let endIndex = str.index(str.startIndex, offsetBy: 7)
String(str[startIndex...endIndex]) // "Strin"
First n chars
let startIndex = str.index(str.startIndex, offsetBy: 3)
String(str[..<startIndex]) // "My "
Last n chars
let startIndex = str.index(str.startIndex, offsetBy: 3)
String(str[startIndex...]) // "String"
str = "My String"
**String At Index **
Swift 2
let charAtIndex = String(str[str.startIndex.advancedBy(3)]) // charAtIndex = "S"
Swift 3
str[str.index(str.startIndex, offsetBy: 3)]
SubString fromIndex toIndex
Swift 2
let subStr = str[str.startIndex.advancedBy(3)...str.startIndex.advancedBy(7)] // subStr = "Strin"
Swift 3
str[str.index(str.startIndex, offsetBy: 3)...str.index(str.startIndex, offsetBy: 7)]
First n chars
let first2Chars = String(str.characters.prefix(2)) // first2Chars = "My"
Last n chars
let last3Chars = String(str.characters.suffix(3)) // last3Chars = "ing"
As an aside note, there are a few functions applyable directly to the Character-chain representation of a String, like this:
var string = "Hello, playground"
let firstCharacter = string.characters.first // returns "H"
let lastCharacter = string.characters.last // returns "d"
The result is of type Character, but you can cast it to a String.
Or this:
let reversedString = String(string.characters.reverse())
// returns "dnuorgyalp ,olleH"
:-)
I just came up with this neat workaround
var firstChar = Array(string)[0]
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.