English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about Kotlin strings, string templates, and some commonly used string properties and functions through examples.
Strings are sequences of characters. For example, "Hello there!" is a string literal.
In Kotlin, all strings are objects of the String class. This means that string literals like "Hello here!" are implemented as instances of that class.
This is how you define a String variable in Kotlin. For example,
val myString = "Hey there!"
Here, myString is a variable of type String.
You can declare a variable of type String and specify its type in one statement, then initialize the variable later in another statement in the program.
val myString: String ... .. ... myString = "Howdy"
To access a string's element (character), use the index access operator. For example,
val myString = "Hey there!" val item = myString[2]
Here, the item variable contains the third character 'y' of the myString string. This is because in Kotlin, the index starts from 0 rather than 1.
val myString = "Hey there!" var item: Char item = myString[0] // item = 'H' item = myString[9] // item = '!' item = myString[10] // Error! String index out of range item = myString[-1] // Error! String index out of range
If you need to iterate over the elements of a string, you can usefor loopEasierImplementation.
Example: String properties and functions val myString = "Hey!" for (item in myString) { println(item) } }
"""
H e y !
Like Java, strings in Kotlin are immutable. This means you cannot change a single character of a string. For example,
var myString = "Hey!" myString[0] = 'h' // Error! Strings
However, if a string variable is declared with the keyword var, the variable can be reassigned. (Recommended reading:Kotlin var vs val)
Example: String properties and functions var myString = "Hey!" println("myString = $myString") myString = "Hello!" println("myString = $myString") }
"""
myString = Hey! myString = Hello!
Literals are the source code representation of fixed values. For example, \Variables).
There are two types of string literals in Kotlin:
Characters may be escaped in the escaped string. For example
val myString = \
Here, \n is an escape character used to insert a newline character at the specified position in the text.
The following is the list of escape characters supported by Kotlin:
\t - Inserting tab
\b - Inserting backspace
\n - Inserting newline
\r - Inserting carriage return
\' - Inserting single quote character
\" - Inserting double quote character
\\\\ - Inserting backslash
\$ - Inserting dollar character
The original string can contain newline characters (not newline characters) and any text. The original string is separated by three double quotes \
Example: String properties and functions val a = for (character in "Hey!") println(character) val myString = "" print(myString) }
"""
for (character in "Hey!") println(character)
You can use the trimMargin() function to remove leading spaces from the original string. For example,
Example: String properties and functions println("Output without using trimMargin function:\n") val a = |Kotlin is interesting. |Kotlin is sponsored and developed by JetBrains. val myString = "" ; println("Output using trimMargin function:\n") println(myString.trimMargin()) }
"""
Output without using trimMargin function: |Kotlin is interesting. |Kotlin is sponsored and developed by JetBrains. Output using trimMargin function: Kotlin is interesting. Kotlin is sponsored and developed by JetBrains.
By default, the trimMargin() function uses | as the margin prefix. However, you can change it by passing a new string to the function.
Example: String properties and functions val a = !!! Kotlin is interesting. !!! Kotlin is sponsored and developed by JetBrains. val myString = "" println(myString.trimMargin("!!! \"\"\"")) }
"""
Kotlin is interesting. Kotlin is sponsored and developed by JetBrains.
Kotlin有一个很棒的功能,称为字符串模板,它允许字符串包含模板表达式。
Kotlin string templates
Example: String properties and functions String template expressions start with the dollar sign $. Here are some examples: 5Example: Kotlin string templates val myInt = ; }
"""
val myString = "myInt = $myInt" 5
println(myString)
Example: String properties and functions This is because the expression $myInt (an expression starting with the $ symbol) in the string is evaluated and concatenated to the string. 5 Example: String templates with original strings 6 val a = val b = val myString = "" |${if (a > b) a else b} }
"""
println("The larger number is: '${myString.trimMargin()}'") 6
The larger number is:
Several string properties and functions - Since literals in Kotlin are implemented as instances of the String class, you can use several methods and properties of this class.
length property -Returns the length of the character sequence of the string.
compareTo function - Compares this string (object) with the specified object. If the object is equal to the specified object, it returns 0.
get function - Returns the character at the specified index. You can use the index access operator instead of the get function because the index access operator internally calls the get function. + plus function+Returns a new string that is obtained by concatenating the string with the string passed to this function. You can use
The operator replaces the plus function because - The operator calls the plus function in the background.
Example: String properties and functions Example1 fun main(args: Array<String>) { Example2 fun main(args: Array<String>) { val s = "Hey there!"1var result: String1println("s The length of the string is '${s}1.length})2result = if (s .compareTo(s1And s2is "Equal" else "Not Equal" // s1.get()2) is equivalent to s1[2] println("The third character is '${s}1.get()2). result = s1.plus(" How are you?") // result = s1 + "How are you?" println("result = " + $result) println("The substring is " + s1.subSequence(4, 7}" }
When the program runs, the output is:
s1The length of the string is 10. String s1And s2Is equal. The third character is y. result = Hey there! How are you? The substring is "the"