This String replace example in Java will show you how to replace String in Java both at character level and by using regular expression. Since String is final in Java every time you replace String you will get a new String object only if your actually replace anything on original String otherwise replace methods of String return same String object. String Class in Java provides 4 methods to replace String in Java. Those methods allow you to replace character from String, replace CharacterSequence from String, replace all occurrence of pattern in String or just first occurrence of any pattern in Java. We will also see important points on String replace method and how to make best use of regular expression while replace string in Java.
This article is on series of my other String article like 2 ways to Split String in Java and how to convert String to Date in Java. String is one of the most important classes in Java and having a good knowledge of String class is mandatory for any Java developer.