Remove last character from string. Swift language

前端 未结 22 1675
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:46

    Swift 3: When you want to remove trailing string:

    func replaceSuffix(_ suffix: String, replacement: String) -> String {
        if hasSuffix(suffix) {
            let sufsize = suffix.count < count ? -suffix.count : 0
            let toIndex = index(endIndex, offsetBy: sufsize)
            return substring(to: toIndex) + replacement
        }
        else
        {
            return self
        }
    }
    

提交回复
热议问题