- 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
Java Program to replace all occurrences of given String with new one
User replaceAll() method to replace all occurrences of a given string. Let’s say the following is our string.
String str = "THIS IS DEMO LINE AND NEW LINE!";
Here, we are replacing all occurrences of string “LINE” to “TEXT”
str.replaceAll("LINE", "TEXT");
The following is the final example.
Example
public class Demo { public static void main(String[] args) { String str = "THIS IS DEMO LINE AND NEW LINE!"; Sytem.out.println("String = "+str); System.out.println("Replacing all occurrence of string LINE..."); System.out.println("Updated string = "+str.replaceAll("LINE", "TEXT")); } }
Output
String = THIS IS DEMO LINE AND NEW LINE! Replacing all occurrence of string LINE... Updated string = THIS IS DEMO TEXT AND NEW TEXT!
- Related Articles
- Java Program to replace only first occurrences of given String with new one
- Java Program to replace all occurrences of a given character with new character
- Java Program to replace all occurrences of a given character in a string
- Replace All Occurrences of a Python Substring with a New String?
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
- How to replace all occurrences of a string with another string in Python?
- C++ program to replace all occurrences of string AB with C without using extra space
- How to replace all occurrences of a word in a string with another word in java?
- Replace all occurrences of specified element of ArrayList with Java Collections
- How to replace all occurrences of a string in JavaScript?
- C program to replace all zeros with one in a given integer.
- Replace one string with another string with Java Regular Expressions
- Java Program to replace one specific character with another
- Replace all words with another string with Java Regular Expressions
- Return a copy of the string with all occurrences of substring old replaced by new in Numpy

Advertisements