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
Java Program to replace one specific character with another
Use the replace() method to replace a specific character with another. Let’s say the following is our string and here we are replacing a whitespace with a $ character.
String str1 = "Orange is the new Black!";
Now, use the replace() method to replace a character with $
str1.replace(' ', '$');
Example
public class Demo {
public static void main(String[] args) {
String str1 = "Orange is the new Black!";
System.out.println("String: "+str1);
String str2 = str1.replace(' ', '$');
System.out.println("Updated string: "+str2);
}
}
Output
String: Orange is the new Black! Updated string: Orange$is$the$new$Black!
Advertisements