This post will discuss how to remove a prefix (or suffix) from a string in Kotlin.

1. Using substring() function

The substring() function returns a substring of the string that starts at the specified index and continues till the end of the string. You can use it as follows to remove a prefix from a string using the startsWith() function.

Download Code

 
To remove a suffix from a string, you can use substring() function with the endsWith() function.

Download Code

2. Using split() function

Another solution is to split the string around the given prefix and return the second value of the resultant array. This can be easily done using the split() function.

Download Code

 
To remove a suffix from a string, split the string around the given suffix and return the first value of the resultant array.

Download Code

That’s all about removing a prefix (or suffix) from a Kotlin String.