Remove last character from string. Swift language

前端 未结 22 1679
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:02

    With the new Substring type usage:

    Swift 4:

    var before: String = "Hello world!"
    var lastCharIndex: Int = before.endIndex
    var after:String = String(before[..<lastCharIndex])
    print(after) // Hello world
    

    Shorter way:

    var before: String = "Hello world!"
    after = String(before[..<before.endIndex])
    print(after) // Hello world
    
    0 讨论(0)
  • 2020-11-30 18:03

    This is a String Extension Form:

    extension String {
    
        func removeCharsFromEnd(count_:Int) -> String {
            let stringLength = count(self)
    
            let substringIndex = (stringLength < count_) ? 0 : stringLength - count_
    
            return self.substringToIndex(advance(self.startIndex, substringIndex))
        }
    }
    

    for versions of Swift earlier than 1.2:

    ...
    let stringLength = countElements(self)
    ...
    

    Usage:

    var str_1 = "Maxim"
    println("output: \(str_1.removeCharsFromEnd(1))") // "Maxi"
    println("output: \(str_1.removeCharsFromEnd(3))") // "Ma"
    println("output: \(str_1.removeCharsFromEnd(8))") // ""
    

    Reference:

    Extensions add new functionality to an existing class, structure, or enumeration type. This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modeling). Extensions are similar to categories in Objective-C. (Unlike Objective-C categories, Swift extensions do not have names.)

    See DOCS

    0 讨论(0)
  • 2020-11-30 18:03
    let str = "abc"
    let substr = str.substringToIndex(str.endIndex.predecessor())  // "ab"
    
    0 讨论(0)
  • 2020-11-30 18:03
    import UIKit
    
    var str1 = "Hello, playground"
    str1.removeLast()
    print(str1)
    
    var str2 = "Hello, playground"
    str2.removeLast(3)
    print(str2)
    
    var str3 = "Hello, playground"
    str3.removeFirst(2)
    print(str3)
    
    Output:-
    Hello, playgroun
    Hello, playgro
    llo, playground
    
    0 讨论(0)
  • 2020-11-30 18:06

    Use the function advance(startIndex, endIndex):

    var str = "45+22"
    str = str.substringToIndex(advance(str.startIndex, countElements(str) - 1))
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题