How to replace a substring inside a string by another one?
SolutionThis example describes how replace method of java String class can be used to replace character or substring by new one.
public class StringReplaceEmp{ public static void main(String args[]){ String str = "Hello World"; System.out.println( str.replace( 'H','W' ) ); System.out.println( str.replaceFirst("He", "Wa") ); System.out.println( str.replaceAll("He", "Ha") ); } }Result
The above code sample will produce the following result.
Wello World Wallo World Hallo World
java_strings.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4