List<T>
8 methods一个泛型的有序元素集合。List 是只读的;MutableList 支持修改。
String.length: Int返回此集合中元素 的数量。
Returns
Int
Example
kotlin
val s = "hello"
println(s.length) // 5String.uppercase(): String将指定元素添加到此列表的末尾。返回 true。
Returns
String
Example
kotlin
val s = "Hello World"
println(s.uppercase()) // "HELLO WORLD"String.lowercase(): String移除指定元素的第一个出现。如果移除成功则返回 true。
Returns
String
Example
kotlin
val s = "Hello World"
println(s.lowercase()) // "hello world"String.split(vararg delimiters: String): List<String>如果在集合中找到该元素,则返回 true。
Parameters
| Name | Type | Description |
|---|---|---|
| element | String | 要检查的元素。 |
Returns
List<String>
Example
kotlin
val parts = "a,b,c".split(",")
// parts == ["a", "b", "c"]
val two = "a-b-c".split("-", limit = 2)
// two == ["a", "b-c"]String.replace(oldValue: String, newValue: String): String返回一个列表,包含对每个元素应用 transform 的结果。
Parameters
| Name | Type | Description |
|---|---|---|
| transform | String | 映射 lambda。 |
| newValue | String | Replacement string. |
Returns
String
Example
kotlin
val s = "Hello, World"
val r = s.replace("World", "Kotlin")
// r == "Hello, Kotlin"String.contains(other: CharSequence): Boolean返回一个列表,仅包含匹配给定谓词的元素。
Parameters
| Name | Type | Description |
|---|---|---|
| predicate | CharSequence | 过滤 lambda。 |
Returns
Boolean
Example
kotlin
val s = "Hello, World"
println(s.contains("World")) // true
println(s.contains("xyz")) // falseString.trim(): String返回一个新列表,所有元素按自然顺序升序排 列。
Returns
String
Example
kotlin
val s = " hello "
println(s.trim()) // "hello"String.startsWith(prefix: String): Boolean按顺序对每个元素执行给定的操作。
Parameters
| Name | Type | Description |
|---|---|---|
| action | String | 操作 lambda。 |
Returns
Boolean
Example
kotlin
val s = "Hello, World"
println(s.startsWith("Hello")) // true
println(s.startsWith("World")) // false