Updated March 27, 2023
Introduction to Kotlin String
In a programming language, a string is a fundamental type of data. The String class in Kotlin contains strings of characters. Literals of the kotlin string are implemented as instances of this type. Kotlin makes use of double quotes to construct a literal series. Strings are story sequences. For examples, “hello there!” is a literal string. In Kotlin all strings are String Type objects. Meaning, as instances of this type, string literals like “Hello there!” are introduced.
How to Initialize String in Kotlin?
String is a multiplicity of characters. Kotlin Strings are more or less like the Java Strings, but some new add-ons do. Below is the syntax for initializing a string in Kotlin:
var str: String = "Hello"
Create an empty string which don’t contain anything inside the string
var a = String()
Here’s how in Kotlin you can describe a String attribute.
Example:
val newString = "Example"
Declaring String in Kotlin Using Various Methods
There are different methods to declare String, some of them are listed below:
#1. Normal Declaration
val name = "Kotlin"
Declaration of Long String:-
val longString = "This is a long string"
#2. Concatenation Operator
In the following example two words or string are combined using + operator which merges the string, example:
fun main(args: Array<String>) {
val str1 = "Today is"
val str2 = " Tuesday"
println(str1 + str2)
}
Another example shows how to determine the length of the declared string.
fun main(args:Array<String>){
var name = "India"
println("String Length: ${name.length}")
}
Functions of Kotlin String
Because Literals in Kotlin are implemented as String class instances, many methods and properties of this type can be used. Some of them are the following:
- Length Property: This type will determine the character in the string and will return the result in the form of output
- compareTo function: This function compares the String(object) with the specified object. Returns 0 if they are equal.
- get function: Returns character at the specified index.
- plus function: Returns a new string by concatenation of this string and the string passed to this function.
- subSequence function: Returns a new character sequence starting at the specified start and end index.
Methods of Kotlin String
There are various methods to declare string in Kotlin. Below are different examples with Code Implementation:
1. In this first example, a simple string is declared, which will initialize the value declared in the variable and will give the output.
Code:
fun main(args:Array<String>) {
val str = "This is an example"
println(str)
}
Output:
2. In this example, we will use concatenate two strings into one string using the concatenation operator +, In the following example we are declaring two strings separately, first in str1 and then the other in str2 and then we are adding both the string together, and printing out the value as an Output.
Code:
fun main(args:Array<String>) {
val str1 = "We are adding"
val str2 = " these two strings"
println(str1 + str2)
}
Output:
3. There is one more example of appending one string with another, which is known as string interpolation in Kotlin and the example will look like as follows.
Code:
fun main(args: Array<String>) {
val name = "Kotlin"
val str = "Hello $name"
println(str)
}
In the above example, the concept is the same which is adding two strings and printing on the output, so the output of the above example will be.
Output:
4. Another example of String interpolation in Kotlin can be shown as follows, where in the complete string can be produced in the Output.
Code:
fun main(args: Array<String>) {
val name = "Kotlin"
val str = "Hello $name"
print("The statement is $str")
}
The output of the above example will add up the two strings which is declared in the program above and the output will be.
Output:
5. In the next example, we can figure out the length of the character which are declared in the program.
Code:
fun main(args: Array<String>) {
val name = "Kotlin"
val str = "Hello $name"
print("The statement is $str. The numbers of character in the statement are ${str.length}")
}
In the above program by using the length method we can determine how many characters are there in the string but if we use $ only before the str so it will result in printing the output without the length of the string present so we need to use the dollar sign on both str and length, to do that we need to put both in the curly braces like above to get the proper output
Output:
6. To give more complex examples let’s see how we can determine the area of a rectangle, don’t worry we will understand step by step what the program will do in this case.
Code:
fun main(args: Array<String>) {
var rect = Rectangle()
rect.length = 7
rect.breadth = 6
print("The Length of the Rectangle is ${rect.length} and breadth is ${rect.breadth}. The Area of rectangle is ${rect.length * rect.breadth}")
}
class Rectangle {
var length: Int = 0
var breadth: Int = 0
}
So to explain better let’s take the above program step by step, so in the first step we are declaring the values in length and breadth after that we are printing the value in output using print statement, in the print statement, we are determining the length of the rectangle using * and multiplying both the values present in length and breadth, so the output of the above program.
Output:
7. Now there is one more thing that you can do in a string which is getting the letter in the string by pointing on the character or randomly checking which character exists at that pointed place as follows.
Code:
fun main(args: Array<String>) {
var string = "This is example in Kotlin"
print(string.get(8))
}
Output:
Conclusion
String in Kotlin is declared in various and several ways to attain and achieve a different output as per requirement. There are multiple ways to declare and use a string in kotlin and many tweaks are there to change the requirement and produce output like getting the length of the character or checking whether the two strings are similar or not or pointing out a character.
Recommended Article
This is a guide to Kotlin String. Here we discuss the Introduction to Kotlin String and its examples along with code implementation and output. You can also go through our other suggested articles to learn more –