InterviewSolution
Saved Bookmarks
| 1. |
How to replace every occurrence of a character in Java? |
|
Answer» To replace every occurrence of a character, use the replace() method in Java. Let’s say the following is the string: TABLES and ChairsAnd for every occurrence of “a”, you NEED to replace it with “z”. The following is the example that would replace every occurrence of “a” with “z”: public class Example { public STATIC void main(String[] args) { // Initial string String STR = "Tables and Chairs"; System.out.println("Initial String = "+str); // REPLACING character String res = str.replace('a', 'z'); // displaying the new string System.out.println("New string = "+res); } }The output: Initial String = Tables and Chairs New string = Tzbles znd Chzirs |
|