Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Replacing a number or string of characters is an interesting thing to do, be it Java, Python or any other programming language. In this article, however, we will focus on Replace in Java in the following order:
There are three methods of replacing Java String.
With the help of these options, you can replace any character in a string.
String replace(): This method returns a new string as output by replacing every occurrence of the character with a new Character.
Syntax:
public Str replace(char oldC, char newCh)
Parameters:
oldCh − old character
newCh − new character
Return Value:
This replaces oldCh with newCh in the string.
Code:
public class Ex1 { 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 'cat' with 'dog': " + S1.replace("cat", "dog")); System.out.println("String after replacing all 't' with 'a': " + S1.replace('t', 'a')); } }
Output:
Original String is ': the cat jumped
String after replacing ‘cat' with 'dog': the dog jumped
String after replacing all’t’ with 'a': ahe quick fox jumped
Java String Replaceall(): This method returns a new string replacing all the sequence of characters matching a regular expression and the replacement string.
Syntax:
public Str replaceAll(String regex, String replacement)
Parameters:
regx: regular expression
replacement: replacement sequence of characters
Code:
public class Ex2 { public static void main(String args[]) { String str = "Java web replace method"; //remove white spaces String str2 = str.replaceAll("s", ""); System.out.println(str2); } }
Javewebreplacemethod
Java String replaceFirst(): This method replaces the first substring of any given string which matches that regular expression.
Syntax:
public Str replaceFirst(String rgex, String replacement)
Parameters:
rgex − the regular expression to which given string needs to matched.
replacement − the string that replaces regular expression.
Code:
public class Ex3 { public static void main(String args[]) { String str = "This is an example of replace"; //Only Replace first 'i' with '7' String str1 = str.replaceFirst("i", "7"); System.out.println(str1); } }
Output:
Th7s is an example of replace.
With this, we come to the end of this article. I hope you got an understanding of how to replace strings and characters.
Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course are designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 21st December,2024 21st December SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 1st March,2025 1st March SAT&SUN (Weekend Batch) | View Details |
edureka.co