Kotlin String - replaceFirst() Function



The Kotlin string replaceFirst() function is used to replace the first occurrences of a substring or character in a string with a specified replacement character. It offers multiple overloaded versions to handle different use cases.

Following are the use cases where we can use the replaceFirst() function −

  • It can replace first occurrences of old characters with new characters.
  • It can replace first occurrences of oldValue substring in this string with the specified newValue string.
  • It replace the first occurrence of the given regular expression regex in this char sequence with specified replacement expression.

Syntax

Following is the syntax of the Kotlin string replaceFirst() function −

// replace first substring with new string.
fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: Boolean = false): String
// replace first character with new character
fun String.replaceFirst(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String
// replace first character sequence of regular expressions
CharSequence.replaceFirst(regex: Regex, replacement: String): String

Parameters

This function accepts the following parameters −

  • oldValue: The substring or character to be replace.
  • newValue: The substring or character to replace First occurrence old value with.
  • ignoreCase: It ignore the case sensitive. By default it is false.

This function also accepts the following parameters for replacement with regular expressions.

  • regex: The regular expression pattern used to find the first match to replace.
  • replacement: The string to replace the matched pattern.

Return value

This function returns a string.

Example 1: Replace First Occurrence of String with Another

Following is the basic example, we replace first occurrence of substring with new string using the replaceFirst() function −

fun main() {
   val string = "ThiS is tutorialSpoint! yes this is"
   // replace First substring
   val newString = string.replaceFirst("This is", "Hello", ignoreCase = true)
   println(newString)
}

Output

Following is the output −

Hello tutorialSpoint! yes this is

Example 2: Replace First Occurrence of Char with Another

Here, in this example, we replace the first occurrence of specified characters in this character sequence, whether it is in uppercase or lowercase −

fun main() {
   val string = "ThiS is tutorialSpoint!"
   // replace first occurence of char
   val newString = string.replaceFirst('s', 'z', ignoreCase = true)
   println(newString)
}

Output

Following is the output −

Thiz is tutorialSpoint!

Example 3: Replace First Occurrence of Digit with Special Char

The below example replaces the first occurrence of a digit with a specified special char using regular expressions −

fun main() {
   val text = "123-456-789"
   val result = text.replaceFirst(Regex("\\d"), "@")
   println(result)
}

Output

Following is the output −

@23-456-789
kotlin_strings.htm
Advertisements