
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Replace All Occurrences of a Given Character in Java
Let’s say the following is our string.
THIS IS DEMO TEXT!
Here, to replace every occurrence of ‘I’ with ‘E’, use the replace() method.
str.replace('I', 'E'));
The following is the complete example to replace all occurrences of a given character with new character.
Example
public class Demo { public static void main(String[] args) { String str = "THIS IS DEMO TEXT!"; System.out.println("String = "+str); System.out.println("Updated string = "+str.replace('I', 'E')); } }
Output
String = THIS IS DEMO TEXT! Updated string = THES ES DEMO TEXT!
Advertisements