![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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
Advertisements