String
7 methods表示一个字符序列。Kotlin 的 String 是不可变的,并桥接到 java.lang.String。
Dictionary subscript [Key] -> Value?返回此字符序列的长度。
Returns
Value?
Example
swift
var d: [String: Int] = ["a": 1, "b": 2]
print(d["a"]!) // 1
print(d["c"]) // nil
d["c"] = 3 // inserts new pairDictionary.count -> Int返回一个字符串,使用默认的区域设置规则将所有字符转换为大写。
Returns
Int
Example
swift
let d = ["a": 1, "b": 2, "c": 3]
print(d.count) // 3Dictionary.keys -> Dictionary.Keys返回一个字符串,使用默认的区域设置规则将所有字符转换为小写。
Returns
Dictionary.Keys
Example
swift
let d = ["a": 1, "b": 2]
let keys = Array(d.keys)
// keys == ["a", "b"] (order may vary)Dictionary.values -> Dictionary.Values根据给定的分隔符匹配项拆分此字符串。
Returns
Dictionary.Values
Example
swift
let d = ["a": 1, "b": 2]
let values = Array(d.values)
// values == [1, 2] (order may vary)Dictionary.updateValue(_ value: Value, forKey key: Key) -> Value?返回一个新字符串,将所有出现的 oldValue 替换为 newValue。
Parameters
| Name | Type | Description |
|---|---|---|
| oldValue | Value | 要替换的子字符串。 |
| newValue | Key | 替换字符串。 |
Returns
Value?
Example
swift
var d = ["a": 1, "b": 2]
let old = d.updateValue(10, forKey: "a")
// old == 1, d["a"] == 10Dictionary.removeValue(forKey: Key) -> Value?如果此字符串包含指定的字符序列,则返回 true。
Parameters
| Name | Type | Description |
|---|---|---|
| other | Key | 要搜索的子字符串。 |
Returns
Value?
Example
swift
var d = ["a": 1, "b": 2]
let removed = d.removeValue(forKey: "a")
// removed == 1, d == ["b": 2]Dictionary.forEach(_ body: ((key: Key, value: Value)) throws -> Void) rethrows返回一个移除了首尾空白字符的字符串。
Parameters
| Name | Type | Description |
|---|---|---|
| body | ((key: Key, value: Value)) -> Void | Closure applied to each pair. |
Returns
Void
Example
swift
let d = ["a": 1, "b": 2]
d.forEach { (k, v) in
print("\(k)=\(v)")
}
// prints:
// a=1
// b=2