Remove last character from string. Swift language

前端 未结 22 1727
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:06

    I'd recommend using NSString for strings that you want to manipulate. Actually come to think of it as a developer I've never run into a problem with NSString that Swift String would solve... I understand the subtleties. But I've yet to have an actual need for them.

    var foo = someSwiftString as NSString
    

    or

    var foo = "Foo" as NSString
    

    or

    var foo: NSString = "blah"
    

    And then the whole world of simple NSString string operations is open to you.

    As answer to the question

    // check bounds before you do this, e.g. foo.length > 0
    // Note shortFoo is of type NSString
    var shortFoo = foo.substringToIndex(foo.length-1)
    

提交回复
热议问题