Skip to content
DeveloperMemos

Using Kotlin's replace function

Kotlin, String manipulation1 min read

In Kotlin, the replace function provides a convenient way to perform string substitution and manipulation. Whether you need to replace specific characters or patterns within a string, this powerful function can streamline your code and improve efficiency. In this article, we'll explore different use cases of the replace function and demonstrate its practical applications in Android development with Kotlin.

Replacing Characters

One common scenario is replacing characters within a string. Let's say we have a string containing a date in the format "MM/DD/YYYY," but we want to change it to "YYYY-MM-DD." We can achieve this easily using the replace function. Here's an example:

1val originalDate = "08/13/2023"
2val formattedDate = originalDate.replace("/", "-")
3println(formattedDate) // Output: 08-13-2023

In the above code snippet, we first define the originalDate string. Then, we call the replace function on it and provide two arguments: the character or sequence we want to replace ("/") and the replacement value ("-"). The replace function returns a new string with the specified replacements.

Replacing Patterns

Not only can we replace individual characters, but we can also replace patterns within strings. This allows for more advanced string manipulation. For instance, let's assume we have a string that contains multiple occurrences of the word "apple," but we want to replace all instances with "orange." The replace function makes it a breeze:

1val originalString = "I have an apple, you have an apple, we all love apples."
2val replacedString = originalString.replace("apple", "orange")
3println(replacedString)
4// Output: I have an orange, you have an orange, we all love oranges.

In the code above, we define the originalString that contains the word "apple" multiple times. By invoking the replace function on it, we specify the pattern to be replaced ("apple") and the replacement value ("orange"). The resulting replacedString will contain all occurrences of "apple" substituted with "orange."

Using Regular Expressions

Kotlin's replace function also supports regular expressions, enabling more sophisticated replacements. Suppose we want to remove all non-alphanumeric characters from a string. We can accomplish this by utilizing regular expressions within the replace function:

1val originalString = "Hello, @World! #HappyCoding"
2val replacedString = originalString.replace("[^A-Za-z0-9]".toRegex(), "")
3println(replacedString) // Output: HelloWorldHappyCoding

In the code snippet above, we define the originalString containing various non-alphanumeric characters. By using the replace function with a regular expression pattern ([^A-Za-z0-9]) and specifying an empty string as the replacement, we effectively remove all non-alphanumeric characters from the string.


Using Kotlin's replace function provides a convenient and efficient way to manipulate and substitute strings in Android development projects. Whether you need to replace characters, patterns, or utilize regular expressions, the replace function is a versatile tool at your disposal. Incorporate it into your codebase to simplify string manipulation tasks and enhance your application's functionality.