我如何从字符串变量使用Swift删除最后一个字符?在文档中找不到。

下面是完整的例子:

var expression = "45+22"
expression = expression.substringToIndex(countElements(expression) - 1)

当前回答

Swift 4.0(也叫Swift 5.0)

var str = "Hello, World"                           // "Hello, World"
str.dropLast()                                     // "Hello, Worl" (non-modifying)
str                                                // "Hello, World"
String(str.dropLast())                             // "Hello, Worl"

str.remove(at: str.index(before: str.endIndex))    // "d"
str                                                // "Hello, Worl" (modifying)

斯威夫特3.0

api变得更加敏捷了,因此Foundation扩展也做了一些改变:

var name: String = "Dolphin"
var truncated = name.substring(to: name.index(before: name.endIndex))
print(name)      // "Dolphin"
print(truncated) // "Dolphi"

或者就地版本:

var name: String = "Dolphin"
name.remove(at: name.index(before: name.endIndex))
print(name)      // "Dolphi"

谢谢Zmey, Rob Allen!

Swift 2.0+方式

有几种方法可以做到这一点:

通过Foundation扩展,尽管不是Swift库的一部分:

var name: String = "Dolphin"
var truncated = name.substringToIndex(name.endIndex.predecessor())
print(name)      // "Dolphin"
print(truncated) // "Dolphi"

使用removeRange()方法(改变名称):

var name: String = "Dolphin"    
name.removeAtIndex(name.endIndex.predecessor())
print(name) // "Dolphi"

使用dropLast()函数:

var name: String = "Dolphin"
var truncated = String(name.characters.dropLast())
print(name)      // "Dolphin"
print(truncated) // "Dolphi"

旧的字符串。索引(Xcode 6 Beta 4 +)方式

由于Swift中的字符串类型旨在提供出色的UTF-8支持,您不能再使用Int类型访问字符索引/范围/子字符串。相反,您使用String。指数:

let name: String = "Dolphin"
let stringLength = count(name) // Since swift1.2 `countElements` became `count`
let substringIndex = stringLength - 1
name.substringToIndex(advance(name.startIndex, substringIndex)) // "Dolphi"

或者(对于一个更实用,但教育意义更小的例子),你可以使用endIndex:

let name: String = "Dolphin"
name.substringToIndex(name.endIndex.predecessor()) // "Dolphi"

注意:我发现这是理解String的一个很好的起点。指数

旧(pre-Beta 4)方式

你可以简单地使用substringToIndex()函数,提供它比String的长度小1:

let name: String = "Dolphin"
name.substringToIndex(countElements(name) - 1) // "Dolphi"

其他回答

Swift 4.0(也叫Swift 5.0)

var str = "Hello, World"                           // "Hello, World"
str.dropLast()                                     // "Hello, Worl" (non-modifying)
str                                                // "Hello, World"
String(str.dropLast())                             // "Hello, Worl"

str.remove(at: str.index(before: str.endIndex))    // "d"
str                                                // "Hello, Worl" (modifying)

斯威夫特3.0

api变得更加敏捷了,因此Foundation扩展也做了一些改变:

var name: String = "Dolphin"
var truncated = name.substring(to: name.index(before: name.endIndex))
print(name)      // "Dolphin"
print(truncated) // "Dolphi"

或者就地版本:

var name: String = "Dolphin"
name.remove(at: name.index(before: name.endIndex))
print(name)      // "Dolphi"

谢谢Zmey, Rob Allen!

Swift 2.0+方式

有几种方法可以做到这一点:

通过Foundation扩展,尽管不是Swift库的一部分:

var name: String = "Dolphin"
var truncated = name.substringToIndex(name.endIndex.predecessor())
print(name)      // "Dolphin"
print(truncated) // "Dolphi"

使用removeRange()方法(改变名称):

var name: String = "Dolphin"    
name.removeAtIndex(name.endIndex.predecessor())
print(name) // "Dolphi"

使用dropLast()函数:

var name: String = "Dolphin"
var truncated = String(name.characters.dropLast())
print(name)      // "Dolphin"
print(truncated) // "Dolphi"

旧的字符串。索引(Xcode 6 Beta 4 +)方式

由于Swift中的字符串类型旨在提供出色的UTF-8支持,您不能再使用Int类型访问字符索引/范围/子字符串。相反,您使用String。指数:

let name: String = "Dolphin"
let stringLength = count(name) // Since swift1.2 `countElements` became `count`
let substringIndex = stringLength - 1
name.substringToIndex(advance(name.startIndex, substringIndex)) // "Dolphi"

或者(对于一个更实用,但教育意义更小的例子),你可以使用endIndex:

let name: String = "Dolphin"
name.substringToIndex(name.endIndex.predecessor()) // "Dolphi"

注意:我发现这是理解String的一个很好的起点。指数

旧(pre-Beta 4)方式

你可以简单地使用substringToIndex()函数,提供它比String的长度小1:

let name: String = "Dolphin"
name.substringToIndex(countElements(name) - 1) // "Dolphi"

补充上述代码,我想删除字符串的开头,但在任何地方都找不到引用。以下是我的做法:

var mac = peripheral.identifier.description
let range = mac.startIndex..<mac.endIndex.advancedBy(-50)
mac.removeRange(range)  // trim 17 characters from the beginning
let txPower = peripheral.advertisements.txPower?.description

这将从字符串的开头修剪17个字符(字符串的总长度是67,从末尾向前移动-50,就得到了它。

let str = "abc"
let substr = str.substringToIndex(str.endIndex.predecessor())  // "ab"

全局的dropLast()函数作用于序列,因此也作用于字符串:

var expression  = "45+22"
expression = dropLast(expression)  // "45+2"

// in Swift 2.0 (according to cromanelli's comment below)
expression = String(expression.characters.dropLast())

一个快速变化的类别:

extension String {
    mutating func removeCharsFromEnd(removeCount:Int)
    {
        let stringLength = count(self)
        let substringIndex = max(0, stringLength - removeCount)
        self = self.substringToIndex(advance(self.startIndex, substringIndex))
    }
}

使用:

var myString = "abcd"
myString.removeCharsFromEnd(2)
println(myString) // "ab"