English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The string replacement method in Kotlin is String.replace(oldValue, newValue). ignoreCase is an optional parameter that can be the third parameter of the replace() method. In this tutorial, we will demonstrate through examples that for each oldValue appearing in the string, we will replace an old value (a string) with a new value (another string), and the usage of ignoring and not ignoring the character case of oldValue.
The syntax of the String.replace method is:
String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String
OldValue - The string oldValue must be replaced with newValue each time it appears in the string.
ignoreCase - [Optional] If true, the character case of oldValue will be ignored when searching for matches in the String. If false, character case will be considered when searching for oldValue matches in the string. The default value of ignoreCase is false.
fun main(args: Array<String>) { var str = "Kotlin Tutorial" - Replace String - Programs" val oldValue = "Programs" val newValue = "Examples" val output = str.replace(oldValue, newValue) print(output) {}
Output Result:
Kotlin Tutorial - Replace String - Examples
fun main(args: Array<String>) { var str = "Kotlin Tutorial" - Replace String - Programs" val oldValue = "PROGRAMS" val newValue = "Examples" val output = str.replace(oldValue, newValue, ignoreCase = true) print(output) {}
Output Result:
Kotlin Tutorial - Replace String - Examples
In this Kotlin tutorial – Kotlin Replace String, we learned how to replace the old value with the new value in a string. And we also discussed the issue of ignoring case when replacing strings in Kotlin examples.