Q:

Example of replace() function in Kotlin

0

replace() Function

The replace() function returns a new string with all occurrences of oldChar (string to be replaced) replaced with newChar (string to replace with).

Syntax:

fun String.replace(
    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 replace() function

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

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

Output:

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

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

total answers (1)

Example of replaceFirst() function in Kotlin... >>
<< Example of matchEntire() function in Kotlin...