You are reading the article Java String Replace(), Replaceall() And Replacefirst() Method updated in September 2023 on the website Nhunghuounewzealand.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Java String Replace(), Replaceall() And Replacefirst() Method
The String Class Java has three types of Replace methods:
replace()
replaceAll()
replaceFirst()
With the help of replace() function in Java, you can replace characters in your string. Lets study each Java string API functions in details:
Java String replace() MethodJava String replace() method replaces every occurrence of a given character with a new character and returns a new string. The Java replace() string method allows the replacement of a sequence of character values.
Syntax:
public Str replace(char oldC, char newC)Parameters:
oldCh − old character
newCh − new character
Return Value
The Java replace() function returns a string by replacing oldCh with newCh.
Example of replace() in Java:
Let’s understand replace() in Java function with an example:
public class Guru99Ex1 { public static void main(String args[]) { String S1 = new String("the quick fox jumped"); System.out.println("Original String is ': " + S1); System.out.println("String after replacing 'fox' with 'dog': " + S1.replace("fox", "dog")); System.out.println("String after replacing all 't' with 'a': " + S1.replace('t', 'a')); } }Expected Output:
Original String is ': the quick fox jumped String after replacing 'fox' with 'dog': the quick dog jumped String after replacing all 't' with 'a': ahe quick fox jumped Java String Replaceall()Java String replaceAll() method finds all occurrences of sequence of characters matching a regular expression and replaces them with the replacement string. At the end of call, a new string is returned by the function replaceAll() in Java.
Signature:
public Str replaceAll(String regex, String replacement)Parameters:
regx: regular expression
replacement: replacement sequence of characters
Example:
public class Guru99Ex2 { public static void main(String args[]) { String str = "Guru99 is a site providing free tutorials"; String str2 = str.replaceAll("\s", ""); System.out.println(str2); } }Expected Output:
Guru99isasiteprovidingfreetutorials Java String replaceFirst()Java String replaceFirst() method replaces ONLY the first substring which matches a given regular expression. Matching of the string starts from the beginning of a string (left to right). At the end of call, a new string is returned by the Java replaceFirst() function.
Syntax
public Str replaceFirst(String rgex, String replacement)Parameters
rgex − the regular expression to which given string need to matched.
replacement − the string that replaces regular expression.
Return Value
This method returns resulting String as an output.
Example of replaceFirst() in Java:
public class Guru99Ex2 { public static void main(String args[]) { String str = "This website providing free tutorials"; String str1 = str.replaceFirst("s", "9"); System.out.println(str1); } }Expected Output:
Thi9 website providing free tutorialsHow to Replace a Character in a String in JAVA?
The syntax for the replace() method is string_name. replace(old_string, new_string)
You're reading Java String Replace(), Replaceall() And Replacefirst() Method
Update the detailed information about Java String Replace(), Replaceall() And Replacefirst() Method on the Nhunghuounewzealand.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!