Create in Kotlin

Find this useful? Support us: Star on GitHub 6
Category: String | Language: Kotlin

Sure, creating a string in Kotlin is quite easy. Here is the basic syntax to create a string in Kotlin:

val str: String = "Hello, World!"

In this example, we are creating a string variable str with the value "Hello, World!".

Here are a few more examples of creating strings in Kotlin:

// Using double quotes
val str1 = "This is a string."

// Using triple quotes for multi-line strings
val str2 = """
This is a multi-line string.
It can span multiple lines.
"""

// Using String constructor
val str3 = String(charArrayOf('h', 'e', 'l', 'l', 'o'))

In the first example, the string is created using double quotes. In the second example, we are using triple quotes to create a multi-line string. And in the third example, we are creating a string using the String constructor by passing an array of characters.

I hope this helps! Let me know if you have any further questions.