Как развернуть строку в kotlin
Completing the CAPTCHA proves you are a human and gives you temporary access to the web property.
What can I do to prevent this in the future?
If you are on a personal connection, like at home, you can run an anti-virus scan on your device to make sure it is not infected with malware.
If you are at an office or shared network, you can ask the network administrator to run a scan across the network looking for misconfigured or infected devices.
Another way to prevent getting this page in the future is to use Privacy Pass. You may need to download version 2.0 now from the Chrome Web Store.
Cloudflare Ray ID: 71ad14e59bbc75cb • Your IP : 82.102.23.104 • Performance & security by Cloudflare
Reverse a string in Kotlin
This article explores different ways to reverse a string in Kotlin.
The string is immutable in Kotlin. This means we can’t reverse a string in-place by shuffling its characters. However, you can create a new string with characters of the original string in reversed order.
1. Using reversed() function
The standard way to reverse a string in Kotlin is with the reversed() function. It returns a string with characters in reversed order.
Kotlin strings
Kotlin strings tutorial shows how to work with strings in Kotlin.
A string is a basic data type in a programming language. In Kotlin, the String class represents character strings. Kotlin string literals are implemented as instances of this class. Kotlin uses double quotes to create string literals.
Kotlin has a rich API for working with strings. It contains plenty of methods for various string operations. Kotlin/Java strings are immutable, which means that all modification operations create new string instead of modifying a string in-place.
Kotlin string example
In the first example, we have a simple Kotlin string example.
The example creates a string, uses a string concatenation operation, and determines the width of the string.
A string literal is created and passed to the s variable. The string is printed to the console with println .
In Kotlin, strings are concatenated with the + operator.
The length of a string is determined with the length attribute.
This is the output.
Kotlin string indexing
A string is a sequence of characters. We can get specific characters from a string with indexing operations.
The example shows how to get the first and last characters of a string. It uses indexing operations and alternative string methods.
The indexes start from zero; therefore, the first character has zero index. The index of the character is placed between square brackets.
The first method returns the first and the last returns the last character of the string.
Kotlin string interpolation
String interpolation is variable substitution with its value inside a string. In Kotlin, we use the $ character to interpolate a variable and $<> to interpolate an expression.
Kotlin string formatting is more powerful than basic interpolation.
The example shows how to do string interpolation in Kotlin.
We have two variables.
The two variables are interpolated within the string; i.e. they are substituted with their values.
Here we get the length of the string. Since it is an expression, we need to put it inside the <> brackets.
This is the output.
Kotlin comparing strings
We can use the == operator and the compareTo method to compare string content.
In the example, we compare two strings.
The == operator compares structural equality, that is, the content of the two strings.
The compareTo method compares two strings lexicographically, optionally ignoring case.
Kotlin string escape characters
The string escaping characters are special characters that perform a specific operation. For instance, the \n characters starts a new line.
The example presents the character escaping in Kotlin.
We insert double qoutes into a string literal by escaping the original function of double quotes.
With \n , we create three lines.
This is the output.
Kotlin string case
Kotlin has methods for working with the case of a string characters.
The example presents four methods: capitalize , toUpperCase , toLowerCase , and decapitalize .
This is the output.
Kotlin empty/blank string
Kotlin distinguishes between empty and blank strings. An empty string does not have any characters, a blank string contains any number of white spaces.
The example tests if a string is bland and empty.
The isEmpty returns true if the string is empty.
The isBlank returns true if the string is blank.
This is the output.
Kotlin string white space stripping
We often need to strip white space characters from a string.
The example presents methods for stripping white spaces from a string.
The trimEnd method removes trailing white spaces.
The trimStart method removes leading white spaces.
The trim method removes both trailing and leading white spaces.
Kotlin string looping
A Kotlin string is a sequence of characters. We can loop this sequence.
The example loops over a string using a for loop, forEach loop, and forEachIndexed loop.
We traverse the string with a for loop and print each of the characters.
We traverse over a loop with forEach and print a byte value of each of the characters.
With forEachIndexed, we print the character with its index.
This is the output.
Kotlin string filtering
The filter method returns a string containing only those characters from the original string that match the given predicate.
The example counts all vowels in the string.
We create an extension function; it returns true for English vowels.
The extension function is called in the filter method.
Kotlin string startsWith/endsWith
The startsWith method returns true if a string starts with the specified prefix and the endsWith returns true if a string ends with the specified character.
In the example, we have a list of words. With the aforementioned methods we find out which words start with ‘t’ and and with ‘k’.
With listOf , we define a list of words.
We call two custom funcions in the filter method.
The startWithT is a custom predicate function which returns true if a string starts with ‘t’.
This is the output.
Kotlin string replace
The replace method returns a new string obtained by replacing all occurrences of the old string with a new string.
The example replaces sunny with rainy. A new modified string is returned. The original string is not modified.
Kotlin string split
The split function cuts the string into a list of strings based on the specified delimiter.
We have a string consisting of birds delimited by comma. We split the strings to get all birds separately.
This is the output.
Kotlin toString
The toString method is called when the object is used in a string context; e.g. it is printed to the console. Its purpose is to provide a string representation of the object.
The example creates a list of city objects. We go through the list and print the objects to the console.
We override the default implementation of toString . It returns a string that a city has the specified population.
This is the output.
Kotlin raw string
A raw string is delimited by triple quotes «»». It does no escaping and can contain newlines and any other characters.
In the example we have a multi-line string, which contains a verse. We strip the indent when the string is printed.
Kotlin string padding
Kotlin has methods for padding strings with a specified character or space.