String Data type Usage and Type Conversion in Kotlin
Kotlin String Overview and Definition
- A string is a collection of characters.
- A String is immutable.
- The type can be either declared explicitly or the compiler itself has the ability to infer the type of the assigned value. There are 2 types of String. They are
- Escaped String
- Raw String
Escaped String
- An escaped String is represented by "Value". An escaped String can have escape characters like "\n", "\t", etc.
- An escaped String is much similar to the Java String.
Raw String
- The raw string is represented by """Value""" and it can contain new lines.
- When a new line is added, by default "|" is added as the delimiter to refer a new line in the raw string. The method ".trimMargin()" removes the delimiter.
- A delimiter of our own choice can also be provided by mentioning the required delimiter in the "trimMargin()" method like "trimMargin(>)"
Syntax and declaration
Thus a String can be declared by 3 ways as mentioned below.
1. Mentioning type explicitly
//Escaped String
val variable_name : String= " "
//Raw String
val variable_name : String= """ """
for example,
val name : String = "Little\nDrops"
val name : String = """Little
|
|
|Drops""".trimMargin()
2. Number type is automatically inferred
//Escaped String
val variable_name = "value"
//Raw String
val variable_name = """ """
for example,
//Data type is automatically inferred
val name = "Little\nDrops"
val name = """Little
|
|
|Drops""".trimMargin()
3. Declaring and Initializing separately
var variable_name : String
variable_name = "value"
for example,
//Declaring and Initializing separately
var name : String
name = "Little\nDrops"
var name : String
name = """Little
|
|
|Drops""".trimMargin()
String Datatype Example Program in Kotlin
// String Datatype Kotlin example program
// Data type Kotlin Programs, Basic Kotlin Programs
fun main(args:Array<String>) {
//Assigning value to String variable explicitly
val a : String = "Little\nDrops"
//Assigned value is inferred automatically by compiler
val b = """Little
|
|
|Drops""".trimMargin()
println("Value of a is $a")
println("Value of b is $b")
}
Sample Output
Value of a is Little
Drops
Value of b is Little
Drops
Kotlin Data Types
- Int Data type Usage and Type Conversion in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Number Data type Usage and Type Conversion in Kotlin
- String Data type Usage and Type Conversion in Kotlin
- Elvis Operator in Kotlin
Read More Articles
- Declare Variables In Kotlin
- Read Data Input using Scanner in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- print and println Data Output in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Logical Operators in Kotlin
- Elvis Operator (?:) in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin