Get nth character of a string in Swift programming language

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

    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)]
    
    0 讨论(0)
  • 2020-11-22 01:48

    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]
    
    0 讨论(0)
  • 2020-11-22 01:48

    Swift 4

    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"
    

    Swift 2 and 3

    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"
    
    0 讨论(0)
  • 2020-11-22 01:50

    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" 
    

    :-)

    0 讨论(0)
  • 2020-11-22 01:52

    I just came up with this neat workaround

    var firstChar = Array(string)[0]
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题