Location>code7788 >text

Kotlin String Tutorial: Deep Understanding and Tips for Use

Popularity:111 ℃/2024-08-02 16:25:11

Kotlin string

Strings are used to store text.

A string contains a collection of characters surrounded by double quotes:

typical example

var greeting = "Hello"

together withJava Unlike a string, you don't have to specify that the variable is a string, and Kotlin is smart enough to understand the double quotes in the above example.greeting The variable is a string.

However, as with other data types, you can specify the type if you insist:

typical example

var greeting: String = "Hello"

Note: If you want to create a string but not assign a value (and assign it later), you must specify the type when you declare the variable:

typical example

That's OK:

var name: String
name = "John"
println(name)

typical example

This creates an error:

var name
name = "John"
println(name)

Access to strings

To access the characters (elements) of a string, the index number must be quoted in square brackets.

The string indexes from the0 Start. In the following example, we access thetxt The first and third elements in the

typical example

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] It's the third element, and so on.

String length

Strings in Kotlin are objects that contain properties and functions that can perform certain operations on strings by writing a dot character after a specific string variable (.). For example, you can use thelength property finds the length of the string:

typical example

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
println("The length of the txt string is: " + )

string function

There are many string functions available, such astoUpperCase() cap (a poem)toLowerCase()

typical example

var txt = "Hello World"
println(()) // exports "HELLO WORLD"
println(()) // exports "hello world"

Compare strings

compareTo(string) function compares two strings, if they are equal then return0

typical example

var txt1 = "Hello World"
var txt2 = "Hello World"
println((txt2)) // outputs 0 (they are equal)

Find strings within strings

indexOf() function to return to the specified text in the string of the first occurrence of the position (including spaces):

typical example

var txt = "Please locate where 'locate' occurs!"
println(("locate"))  // exports 7

Remember, Kotlin counts from zero.

0 is the first position in the string.1 It's the second one.2 It's the third ......

Quotation marks in strings

To use quotes in a string, use single quotes ('):

typical example

var txt1 = "It's alright"
var txt2 = "That's great"

string concatenation

You can use the+ Operators add them together to form a new string. This is called concatenation:

typical example

var firstName = "John"
var lastName = "Doe"
println(firstName + " " + lastName)

Note that we added an empty text (" ") to print in thefirstName cap (a poem)lastName Creates a space between

You can also use theplus() function joins two strings:

typical example

var firstName = "John "
var lastName = "Doe"
println((lastName))

String templates/interpolation

In addition to concatenation, you can also use "string templates", which are an easy way to add variables and expressions to strings.

Simply use the$ Symbolic references to variables:

typical example

var firstName = "John"
var lastName = "Doe"
println("My name is $firstName $lastName")

"String templates are a popular feature of Kotlin because they reduce the amount of code. For example, you don't have to add a string template to thefirstName cap (a poem)lastName Specify spaces between them.

ultimate

To make it easier for peeps on other devices and platforms to view past articles:

WeChat Search:Let us CodingFollow to get the latest articles tweeted

If you find it helpful, please like, favorite and follow it!