One of the common operation when working with strings is to extract a substring of another string. There are several subString methods added to Kotlin String class as extension functions and we’re going to see them one-by-one.

So, in this quick article, we’ll talk about how to use different substring methods in Kotlin.

.subString(startIndex: Int) Method

The above subString method returns a new string that starts from the specified startIndex and ends at right before the length of the calling string.

Now, if you try to run the above code you’ll notice that the startIndex start from zero and it is inclusive.

.subString(startIndex: Int, endIndex: Int) Method

You can also specify the start and endIndex of the source string to extract the substring. The complete syntax is as follows:

In the above example, the startIndex is where you want to get a substring from and endIndex specifies the end position in the source string.

Note: Space is also a valid character between the MY_NAME string.

.substringAfter(delimiter: String, missingDelimiterValue: String = this) Method

At some point, you may need to get a substring using a defined delimiter parameter. The result you get is the substring after the first appearance of delimiter. If the source string does not contain the delimiter, then the missingDelimeterValue will be returned which defaults to the source string.

Let’s take a look at the complete syntax of substringAfter method:

The above program will successfully give you the mp4 extension name of the file. But if we change the delimiter value to something which does not exist inside the source string then the missingDelimiterValue will be returned which is Extension Not Found.

You can also pass the delimiter as a Char.

.substringBefore(delimiter: String, missingDelimiterValue : String = this) Method

Above, in the last method, I mention how we can get the substring after the first appearance of delimiter. Well, with this method we can get the substring before the first appearance of delimiter.

Here’s a concrete example of using the substringBefore method.

You can see, the above program returns the substring before the appearance of @ character.

Same as the above method you can pass the delimiter parameter as a Char.

.substringAfterLast(delimiter : String, missingDelimiterValue : String= this) Method

I know, the name of this method had a similarity to substringAfter but it works a little different than the other. So, this substring method starts finding the delimiter value from the right side of the source string and returns a substring after the last occurrence of delimiter.

Now if I had a file path which contains the two same delimeter and I only want to get the substring after the last delimiter then this method is perfect for our logic.

Let’s see a working example:

Awesome! the substringAfterLast method successfully gives the file extension although the FILE_PATH contains two delimiter value well, that’s because the method starts searching from right-side of source string.

.substringBeforeLast(delimiter: String, missingDelimiterValue : String= this) Method

The only difference between this substring method and previous: this method returns the substring before the last occurrence of the delimiter. The method also starts searching from right-side and when finds the first delimiter it returns the left-sided substring which not even used for searching.

I know! that’s not a valid email address but this is only for example. The moment substringBeforeLast method finds the first @ delimiter from the right side even though the MY_EMAIL has two @ delimiters. It’ll return the remaining left side substring without going further.

.substring(range: IntRange) Method

Now if you want a substring specified by the given Range indices then this method is what you’re looking for. Internally this method calls the substring(startIndex , endIndex) method. Here’s the implementation of substring(range: IntRange).

The following shows the working example:

There you have it! It’s really easy to extract a substring in Kotlin with the above extension functions. I hope, I educate you more in Kotlin.

Thank you for being here and keep reading…


1 Comment

Write A Comment