String
8 methodsRepresents a character sequence. Kotlin String is immutable and bridges to java.lang.String.
String.length: IntReturns the length of this character sequence.
Returns
Int
Example
val s = "hello"
println(s.length) // 5String.uppercase(): StringReturns a string with all characters converted to uppercase using default locale rules.
Returns
String
Example
val s = "Hello World"
println(s.uppercase()) // "HELLO WORLD"String.lowercase(): StringReturns a string with all characters converted to lowercase using default locale rules.
Returns
String
Example
val s = "Hello World"
println(s.lowercase()) // "hello world"String.split(vararg delimiters: String): List<String>Splits this string around matches of the given delimiters.
Parameters
| Name | Type | Description |
|---|---|---|
| delimiters | String | Delimiter strings to split on. |
Returns
List<String>
Example
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): StringReturns a new string with all occurrences of oldValue replaced with newValue.
Parameters
| Name | Type | Description |
|---|---|---|
| oldValue | String | Substring to replace. |
| newValue | String | Replacement string. |
Returns
String
Example
val s = "Hello, World"
val r = s.replace("World", "Kotlin")
// r == "Hello, Kotlin"String.contains(other: CharSequence): BooleanReturns true if this string contains the specified char sequence.
Parameters
| Name | Type | Description |
|---|---|---|
| other | CharSequence | Substring to search for. |
Returns
Boolean
Example
val s = "Hello, World"
println(s.contains("World")) // true
println(s.contains("xyz")) // falseString.trim(): StringReturns a string with leading and trailing whitespace removed.
Returns
String
Example
val s = " hello "
println(s.trim()) // "hello"String.startsWith(prefix: String): BooleanReturns true if this string starts with the specified prefix.
Parameters
| Name | Type | Description |
|---|---|---|
| prefix | String | Prefix to check. |
Returns
Boolean
Example
val s = "Hello, World"
println(s.startsWith("Hello")) // true
println(s.startsWith("World")) // false