删除字符串中的最后两个字符(Swift 3.0)

时间:2022-03-14 17:14:36

Is there a quick way to remove the last two characters in a String in Swift 3.0? I see there is a simple way to remove the last character as clearly noted here. Do you know how to remove the last two characters? Thanks!

在Swift 3.0中有快速删除字符串中最后两个字符的方法吗?我看到有一种简单的方法可以删除最后一个字符。你知道怎么去掉最后两个字符吗?谢谢!

4 个解决方案

#1


45  

var name: String = "Dolphin"
let endIndex = name.index(name.endIndex, offsetBy: -2)
let truncated = name.substring(to: endIndex)
print(name)      // "Dolphin"
print(truncated) // "Dolph"

#2


86  

You can use the method dropLast(n:) on the characters to remove any number of characters:

您可以在字符上使用dropLast(n:)方法来删除任意数量的字符:

let str = "0123456789"
let result = String(str.characters.dropLast(2))   // "01234567"

As an extension:

作为一个扩展:

extension String {
    func dropLast(_ n: Int = 1) -> String {
        return String(characters.dropLast(n))
    }
    var dropLast: String {
        return dropLast()
    }
}

let str = "0123456789"

let result = str.dropLast(2)     // "01234567"
let result2 = result.dropLast    // "0123456"

update: Xcode 9 • Swift 4

更新:Xcode 9•Swift 4

Now String conforms to RangeReplaceableCollection so now you can use Array's method dropLast straight in the String and therefore an extension it is not necessary anymore. The only difference is that it returns a Substring. If you need a String you need to initialize a new one from it:

现在字符串符合RangeReplaceableCollection,现在可以在字符串中直接使用Array方法dropLast,因此不再需要扩展。唯一的区别是它返回子字符串。如果你需要一个字符串,你需要初始化一个新的字符串:

let string = "0123456789"
let substring1 = string.dropLast(2)         // "01234567"
let substring2 = substring1.dropLast()      // "0123456"
let result = String(substring2.dropLast())  // "012345"

#3


17  

swift 4:

斯威夫特4:

let str = "Hello, playground"
let newSTR1 = str.dropLast(3)
print(newSTR1) 

output: "Hello, playgro"

//---------------//

let str = "Hello, playground"
let newSTR2 = str.dropFirst(2)
print(newSTR2)

output: "llo, playground"

#4


1  

Use removeSubrange(Range<String.Index>) just like:

使用removeSubrange(范围< String.Index >)就像:

var str = "Hello, playground"
str.removeSubrange(Range(uncheckedBounds: (lower: str.index(str.endIndex, offsetBy: -2), upper: str.endIndex)))

This will crash if the string is less than 2 characters long. Is that a requirement for you?

如果字符串长度小于2个字符,将会崩溃。这是你的要求吗?

#1


45  

var name: String = "Dolphin"
let endIndex = name.index(name.endIndex, offsetBy: -2)
let truncated = name.substring(to: endIndex)
print(name)      // "Dolphin"
print(truncated) // "Dolph"

#2


86  

You can use the method dropLast(n:) on the characters to remove any number of characters:

您可以在字符上使用dropLast(n:)方法来删除任意数量的字符:

let str = "0123456789"
let result = String(str.characters.dropLast(2))   // "01234567"

As an extension:

作为一个扩展:

extension String {
    func dropLast(_ n: Int = 1) -> String {
        return String(characters.dropLast(n))
    }
    var dropLast: String {
        return dropLast()
    }
}

let str = "0123456789"

let result = str.dropLast(2)     // "01234567"
let result2 = result.dropLast    // "0123456"

update: Xcode 9 • Swift 4

更新:Xcode 9•Swift 4

Now String conforms to RangeReplaceableCollection so now you can use Array's method dropLast straight in the String and therefore an extension it is not necessary anymore. The only difference is that it returns a Substring. If you need a String you need to initialize a new one from it:

现在字符串符合RangeReplaceableCollection,现在可以在字符串中直接使用Array方法dropLast,因此不再需要扩展。唯一的区别是它返回子字符串。如果你需要一个字符串,你需要初始化一个新的字符串:

let string = "0123456789"
let substring1 = string.dropLast(2)         // "01234567"
let substring2 = substring1.dropLast()      // "0123456"
let result = String(substring2.dropLast())  // "012345"

#3


17  

swift 4:

斯威夫特4:

let str = "Hello, playground"
let newSTR1 = str.dropLast(3)
print(newSTR1) 

output: "Hello, playgro"

//---------------//

let str = "Hello, playground"
let newSTR2 = str.dropFirst(2)
print(newSTR2)

output: "llo, playground"

#4


1  

Use removeSubrange(Range<String.Index>) just like:

使用removeSubrange(范围< String.Index >)就像:

var str = "Hello, playground"
str.removeSubrange(Range(uncheckedBounds: (lower: str.index(str.endIndex, offsetBy: -2), upper: str.endIndex)))

This will crash if the string is less than 2 characters long. Is that a requirement for you?

如果字符串长度小于2个字符,将会崩溃。这是你的要求吗?