Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
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.
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.
val newString = “Example”
There are different methods to declare String, some of them are listed below:
val name = “Kotlin”
Declaration of Long String:-
val longString = “This is a long string”
Popular Course in this category
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}”)
}
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:
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.
fun main(args:Array<String>) {
val str = “This is an example”
println(str)
}
2. In this example, we will use concatenate two string into one string using 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.
fun main(args:Array<String>) {
val str1 = “We are adding”
val str2 = ” these two strings”
println(str1 + str2)
}
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.
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.
4. Another example of String interpolation in Kotlin can be shown as follows, where in the complete string can be produced in the Output.
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 string which are declared in the program above and the output will be.
5. In the next example, we can figure out the length of the character which are declared in the program.
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 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
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.
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.
7. Now there is one more thing which you can do in 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.
fun main(args: Array<String>) {
var string = “This is example in Kotlin”
print(string.get(8))
}
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 to point out a character.
Learn more about programming languages at mcp
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.