Kotlin String
Summarize of Kotlin String.
3. String
1. String Equality
String are compared with == operator with check for their structural equality.
1 | val str1 = "Hello, World" |
Referential equality is checked with ===
operator.
1 | val str1 = """ |
2. String Literals
Kotlin has two types of string literals:
- Escaped string
- Raw string
Escaped string handles special characters by escaping them. The following escape sequences are supported: \t, \b, \n, \r, \', \", \\ \$
, also use Unicode \uFF00
.
1 | val a = "Hello \n Hello" |
The result is like below.
1 | Hello |
Raw string delimited by """"
.
1 | val text = """ |
Leading whitespace can be removed with trimMargin() function.
1 | val text = """ |
3. Elements of String
Element of String are characters that can be accessed by the indexing operation string[index].
1 | val str = "Hello, World!" |
String elements can be iterated with a for loop.
1 | for(x in str){ |
4. String Templates
String template starts with a dollar sign $ and consists of either a variable name:
Or an arbitrary expression in curly braces:
1 | val i = 10 |