Remove last character from string. Swift language

前端 未结 22 1713
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 17:50

    Short answer (valid as of 2015-04-16): removeAtIndex(myString.endIndex.predecessor())

    Example:

    var howToBeHappy = "Practice compassion, attention and gratitude. And smile!!"
    howToBeHappy.removeAtIndex(howToBeHappy.endIndex.predecessor())
    println(howToBeHappy)
    // "Practice compassion, attention and gratitude. And smile!"
    

    Meta:

    The language continues its rapid evolution, making the half-life for many formerly-good S.O. answers dangerously brief. It's always best to learn the language and refer to real documentation.

提交回复
热议问题