Q:

Example of replaceFirst() function in Kotlin

0

replaceFirst() Function

The replaceFirst() function returns a new string with the first occurrence of oldChar (string to be replaced) replaced with newChar (string to replace with).

Syntax:

fun String.replaceFirst(
    oldChar: Char,
    newChar: Char,
    ignoreCase: Boolean = false
): String

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Kotlin program to demonstrate the example of replaceFirst() function

// Example of replaceFirst() function
fun main()
{
	// Demonstrating the replaceFirst() function
	val pattern = Regex("Hi")

	// Replace all "Hi" with "Hello"
	println(pattern.replaceFirst("Hi, there.", "Hello"))
	println(pattern.replaceFirst("Hi Alex! Please say Hi to Boss.", "Hello"))
	println(pattern.replaceFirst("IncludeHelp", "Okay"))
	println()
}

Output:

Hello, there.
Hello Alex! Please say Hi to Boss.
IncludeHelp

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Example of split() function in Kotlin... >>
<< Example of replace() function in Kotlin...