Java String replace() Method
What is a string object in Java?
In Java, a string object is an object that represents a sequence of characters. A string object is created using the String class, which is a built-in class in Java.You can also create a string object using a string literal, which is a sequence of characters enclosed in double quotes.
There are two ways to create a string in Java:
- Using a string literal: A string literal is a sequence of characters enclosed in double quotes.
- Using the String class: The String class has a constructor that allows you to create a string from an array of characters or from another string.
To know more about String Class in java read the complete article.
String replace() method.
The replace method is a built-in method of the String class in Java that allows you to replace all occurrences of a specified string or character with another string or character. The replace method returns a new string with the specified replacements applied. It does not modify the original string.
- Note that the replace and replaceAll methods are case-sensitive, so they will only replace strings or characters that match the case of the search strings. If you want to perform a case-insensitive replacement, you can use the replaceIgnoreCase method or convert the string to upper or lower case before performing the replacement.
Syntax:
public String replace(char oldChar,char newChar)
Parameters:
oldChar - the old character. newChar - the new character.
Let’s look at a string-related Java program where the Java String Replace() Method is used to perform an operation on the given string.
Example: Java String replace() Method
public class Main { public static void main(String[] args) { String str = "PrepInsta Prime"; String newStr1 = str.replace('i', 'I'); String newStr2 = str.replace("Prime", "platform"); System.out.println("Original string: " + str); System.out.println("Replaced character: " + newStr1); System.out.println("Replaced string: " + newStr2); } }
Output
Original string: PrepInsta Prime Replaced character: PrepInsta PrIme Replaced string: PrepInsta platform
Example 2 : Java String replace() Method
public class Main { public static void main(String[] args) { String str = "Hello World"; String newStr = str.replace('l', 'L'); System.out.println("Original string: " + str); System.out.println("Replaced character: " + newStr); } }
Output
Original string: Hello World Replaced character: HeLLo WorLd
Example 3: how to use the length property:
public class Main { public static void main(String[] args) { String str = "Hello World"; // Replace all vowels with an asterisk and all spaces with underscores String newStr = str.replaceAll("[aeiou]", "*").replace(" ", "_"); System.out.println("Original string: " + str); System.out.println("Modified string: " + newStr); } }
Output
Original string: Hello World Modified string: H*ll*_W*rld
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment