- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Replace one string with another string with Java Regular Expressions
To replace one string with another string using Java Regular Expressions, we need to use the replaceAll() method. The replaceAll() method returns a String replacing all the character sequence matching the regular expression and String after replacement.
Declaration − The java.lang.String.replaceAll() method is declared as follows −
public String replaceAll(String regex, String replaced)
Let us see a program to replace one string with another string using Java Regular Expressions −
Example
public class Example { public static void main( String args[] ) { String str = new String("Good Harry Good"); System.out.println( "Initial String : "+ str); // replacing "Good" with "Bad" str = str.replaceAll( "Good" , "Bad" ); System.out.println( "The String after substitution : "+str ); } }
Output
Initial String : Good Harry Good The String after substitution : Bad Harry Bad
- Related Articles
- Replace all words with another string with Java Regular Expressions
- Replace String with another in java.
- Search and Replace with Java regular expressions
- Replace '*' with '^' with Java Regular Expressions
- Replace part of a string with another string in C++
- How to replace all occurrences of a string with another string in Python?
- Java Program to replace one specific character with another
- How to extract data from a string with Python Regular Expressions?
- Java Program to replace all occurrences of given String with new one
- Validate Phone with Java Regular Expressions
- MySQL Regular expressions: How to match digits in the string with d?
- How to replace total string if partial string matches with another string in R data frame column?
- Java Program to replace only first occurrences of given String with new one
- How to replace all occurrences of a word in a string with another word in java?
- Remove Leading Zeroes from a String in Java using regular expressions

Advertisements