ADVERTISEMENT
ADVERTISEMENT

Example of replaceFirst() function in Kotlin

Learn about the replaceFirst() function in Kotlin, and demonstrate the example of the replaceFirst() function.
Submitted by IncludeHelp, on March 25, 2022

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

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

Kotlin Regular Expression (Regex) Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.