Scala String replaceFirst() method with example
Last Updated :
03 Oct, 2019
The replaceFirst() method is same as replaceAll but here only the first appearance of the stated sub-string will be replaced.
Method Definition: String replaceFirst(String regex, String replacement)
Return Type: It returns the stated string after replacing the first appearance of stated regular expression with the string we provide.
Example #1:
object GfG
{
def main(args : Array[String])
{
val result = "csoNidhimsoSingh" .replaceFirst( ".so" , "##" )
println(result)
}
}
|
Example #2:
object GfG
{
def main(args : Array[String])
{
val result = "NidhimsoSinghcso" .replaceFirst( ".so" , "*" )
println(result)
}
}
|
Example #3:
object GfG
{
def main(args : Array[String])
{
val result = "fsoNidhimsoSinghcso" .replaceFirst( ".so" , "*" )
println(result)
}
}
|
Output:
*NidhimsoSinghcso
Share your thoughts in the comments
Please Login to comment...