Remove last character from string. Swift language

前端 未结 22 1678
Happy的楠姐
Happy的楠姐 2020-11-30 17:33

How can I remove last character from String variable using Swift? Can\'t find it in documentation.

Here is full example:

var expression = \"45+22\"
e         


        
相关标签:
22条回答
  • 2020-11-30 18:07

    Swift 4:

    let choppedString = String(theString.dropLast())
    

    In Swift 2, do this:

    let choppedString = String(theString.characters.dropLast())
    

    I recommend this link to get an understanding of Swift strings.

    0 讨论(0)
  • 2020-11-30 18:08

    Swift 4/5

    var str = "bla"
    str.removeLast() // returns "a"; str is now "bl"
    
    0 讨论(0)
  • 2020-11-30 18:10

    Use the function removeAtIndex(i: String.Index) -> Character:

    var s = "abc"    
    s.removeAtIndex(s.endIndex.predecessor())  // "ab"
    
    0 讨论(0)
  • The easiest way to trim the last character of the string is:

    title = title[title.startIndex ..< title.endIndex.advancedBy(-1)]
    
    0 讨论(0)
提交回复
热议问题