

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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
- 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?
- 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.
- How to replace all occurrences of a word in a string with another word in java?
- Replace one string with another string with Java Regular Expressions
- Java Program to replace one specific character with another
- Remove new lines from a string and replace with one empty space PHP?
- 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
- Java program to count occurrences of a word in string
Advertisements