- 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 a given character in a string
Use replace() method to replace all occurrences of a given character in a string.
Here is our string.
String str = "THIS IS DEMO LINE $$$ NEW LINE";
Let us now replace all occurrences of character $ with *
str.replace("$", "*")
The following is the complete example that replace all occurrences of $ with *
Example
public class Demo { public static void main(String[] args) { String str = "THIS IS DEMO LINE $$$ NEW LINE"; System.out.println("String = "+str); System.out.println("Replacing all occurrence of given character..."); System.out.println("Updated string = "+str.replace("$", "*")); } }
Output
String = THIS IS DEMO LINE $$$ NEW LINE Replacing all occurrence of given character... Updated string = THIS IS DEMO LINE *** NEW LINE
- Related Articles
- Java Program to replace all occurrences of a given character with new character
- Java Program to replace all occurrences of given String with new one
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
- C program to replace all occurrence of a character in a string
- How to replace all occurrences of a string in JavaScript?
- 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?
- How to replace all occurrences of a string with another string in Python?
- Replace All Occurrences of a Python Substring with a New String?
- Python program to replace all Characters of a List except the given character
- Java Program to Replace the Spaces of a String with a Specific Character
- C# program to replace n-th character from a given index in a string
- Replace Character in a String in Java without using replace() method
- Java program to find the Frequency of a character in a given String
- C# Program to replace a special character from a String

Advertisements