Kotlin – String Replace

The basic String Replace method in Kotlin is String.replace(oldValue, newValue). ignoreCase is an optional argument, that could be sent as third argument to the replace() method. In this tutorial, we shall go through examples where we shall replace an old value (string) with a new value (another string) for each occurrence of oldValue in a String, ignoring and not ignoring oldValue’s character case.

Syntax of String replace()

Syntax of String.replace method is

oldValue : The string which has to be replaced with newValue for each occurrence of oldValue in String.

ignoreCase : [Optional] If true, character case of oldValue would be not be taken into account to find a match in String. If false, character case would be considered while finding a match for oldValue in the String. The default value of ignoreCase is false.

Example – Kotlin Replace Substring

In the following Kotlin Example, we shall replace substring Programs withExamples value in the string,Kotlin Tutorial - Replace String - Programs

Example – Kotlin String Replace – Ignore Case

In the following Kotlin Example, we shall replacePROGRAMS string withExamples value in the string,Kotlin Tutorial - Replace String - Programs ignoring character case. To ignore case, we passignoreCase = true  as third argument.

Conclusion

In this Kotlin TutorialKotlin Replace String, we have learnt how to replace a old value with a new value in a string. We have also dealt with ignoring case while replacing the string with Kotlin Examples.