- 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 all words with another string with Java Regular Expressions
To replace all words 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 all words with another string with 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 all words with with "Bad" str = str.replaceAll( "\w+" , "Bad" ); System.out.println( "The String after substitution : "+str ); } }
Output
Initial String : Good Harry Good The String after substitution : Bad Bad Bad
- Related Articles
- Replace one string with another string with Java Regular Expressions
- Search and Replace with Java regular expressions
- Replace '*' with '^' with Java Regular Expressions
- Replace String with another in java.
- Validate Phone with Java Regular Expressions
- How to replace all occurrences of a string with another string in Python?
- How to replace all occurrences of a word in a string with another word in java?
- Validate city and state with Java Regular Expressions
- Validate the ZIP code with Java Regular expressions
- Working with simple groups in Java Regular Expressions
- Working with Matcher.start() method in Java Regular Expressions
- Working with Matcher.end() method in Java Regular Expressions
- Replace part of a string with another string in C++
- Finding a Match Within Another Match Java regular expressions
- Java Program to replace all occurrences of given String with new one

Advertisements