HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Strings are used for storing text
Strings are used for storing text.
A string contains a collection of characters surrounded by double quotes:
var greeting = "Hello"
Unlike Java, you do not have to specify that the variable should be a String
. Kotlin is smart enough to understand that the greeting variable in the example
above is a String
because of the double quotes.
However, just like with other data types, you can specify the type if you insist:
var greeting: String = "Hello"
String
without assigning the value (and assign the value later), you must specify the type while declaring the variable:
This works fine:
var name: String
name = "John"
println(name)
This will generate an error:
var name
name = "John"
println(name)
To access the characters (elements) of a string, you must refer to the index number inside square brackets.
String indexes start with 0. In the example below, we access the first and third element in
txt
:
var txt = "Hello World"
println(txt[0]) // first element (H)
println(txt[2]) // third element (l)
[0] is the first element. [1] is the second element, [2] is the third element, etc.
A String in Kotlin is an object, which contain properties and functions that can perform certain operations on strings,
by writing a dot character (.
) after the specific string variable. For example, the length of a string can be found with the length
property:
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
println("The length of the txt string is: " + txt.length)
There are many string functions available, for example toUpperCase()
and toLowerCase()
:
var txt = "Hello World"
println(txt.toUpperCase()) // Outputs "HELLO WORLD"
println(txt.toLowerCase()) // Outputs "hello world"
The compareTo(string)
function
compares two strings and returns 0 if both are equal:
var txt1 = "Hello World"
var txt2 = "Hello World"
println(txt1.compareTo(txt2)) // Outputs 0 (they are equal)
The indexOf()
function returns the index (the position)
of the first occurrence of a specified text in a string
(including whitespace):
var txt = "Please locate where 'locate' occurs!"
println(txt.indexOf("locate")) // Outputs 7
Remember that Kotlin counts positions from zero.
0 is the first position in a
string, 1 is the second, 2 is the third ...
To use quotes inside a string, use single quotes ('
):
var txt1 = "It's alright"
var txt2 = "That's great"
The +
operator can be used between strings to add them together to make a new
string. This is called concatenation:
var firstName = "John"
var lastName = "Doe"
println(firstName + " " + lastName)
Note that we have added an empty text (" ") to create a space between firstName and lastName on print.
You can also use the plus()
function to concatenate two strings:
var firstName = "John "
var lastName = "Doe"
println(firstName.plus(lastName))
Instead of concatenation, you can also use "string templates", which is an easy way to add variables and expressions inside a string.
Just refer to the variable with the $
symbol:
var firstName = "John"
var lastName = "Doe"
println("My name is $firstName $lastName")
"String Templates" is a popular feature of Kotlin, as it reduces the amount of code. For example, you do not have to specify a whitespace between firstName and lastName, like we did in the concatenation example.