Scala String replaceFirst() method with example Last Updated : 03 Oct, 2019 Comments Improve Suggest changes Like Article Like Report 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: Scala // Scala program of replaceFirst() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Applying replaceFirst method val result = "csoNidhimsoSingh".replaceFirst(".so", "##") // Displays output println(result) } } Output: ##NidhimsoSingh Example #2: Scala // Scala program of replaceFirst() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Applying replaceFirst method val result = "NidhimsoSinghcso".replaceFirst(".so", "*") // Displays output println(result) } } Output: Nidhi*Singhcso Example #3: Scala // Scala program of replaceFirst() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Applying replaceFirst method val result = "fsoNidhimsoSinghcso".replaceFirst(".so", "*") // Displays output println(result) } } Output: *NidhimsoSinghcso Comment More infoAdvertise with us Next Article Scala String replaceFirst() method with example N nidhi1352singh Follow Improve Article Tags : Scala Scala Scala-Method Scala-Strings Similar Reads Scala String replace() method with example The replace() method is used to replace the old character of the string with the new one which is stated in the argument. Method Definition: String replace(char oldChar, char newChar) Return Type: It returns the stated string after replacing the old character with the new one. Example #1: Scala // S 1 min read Scala String replaceAll() method with example The replaceAll() method is used to replace each of the stated sub-string of the string which matches the regular expression with the string we supply in the argument list. Method Definition: String replaceAll(String regex, String replacement) Return Type: It returns the stated string after replacing 1 min read Scala - String Methods with Examples In Scala, as in Java, a string is a sequence of characters. In Scala, objects of String are immutable which means a constant and cannot be changed once created. In the rest of this section, we discuss the important methods of java.lang.String class. char charAt(int index): This method is used to ret 12 min read Scala String trim() method with example The trim() method is utilized to omit the leading and trailing spaces in the stated string. Method Definition: String trim() Return Type: It returns the stated string after removing all the white spaces. Example: 1# Scala // Scala program of trim() // method // Creating object object GfG { // Main m 1 min read Scala Set min() method with example The max() method is utilized to find the smallest element of all the elements in the stated list. Method Definition: def min: A Return Type: It returns the smallest of all the elements in the stated list. Example #1: Scala // Scala program of min() // method // Creating object object GfG { // Main m 1 min read Like