Remove last character from string. Swift language

前端 未结 22 1684
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:00

    The global dropLast() function works on sequences and therefore on Strings:

    var expression  = "45+22"
    expression = dropLast(expression)  // "45+2"
    
    // in Swift 2.0 (according to cromanelli's comment below)
    expression = String(expression.characters.dropLast())
    

提交回复
热议问题