OneBite.Dev - Coding blog in a bite size

remove a substring from a string in java

Code snippet on how to remove a substring from a string in java

String originalString = "Coders that don't code";
String substring = "that ";
 
String newString = originalString.replace(substring, "");
System.out.println(newString);

This code begins by declaring the two strings we will use: one for the original string, and one for the substring we will remove from it. Then, it uses the .replace method to replace the substring with an empty string. Finally, it prints the output of the .replace method, which is the original string with the substring removed.

java