How can I remove a substring from a given String?
replace('regex', 'replacement');
replaceAll('regex', 'replacement');
In your example,
String hi = "Hello World!"
String no_o = hi.replaceAll("o", "");
You could easily use String.replace()
:
String helloWorld = "Hello World!";
String hellWrld = helloWorld.replace("o","");
You can use StringBuffer
StringBuffer text = new StringBuffer("Hello World");
text.replace( StartIndex ,EndIndex ,String);
Check out Apache StringUtils:
static String replace(String text, String searchString, String replacement)
Replaces all occurrences of a String within another String.static String replace(String text, String searchString, String replacement, int max)
Replaces a String with another String inside a larger String, for the first max values of the search String.static String replaceChars(String str, char searchChar, char replaceChar)
Replaces all occurrences of a character in a String with another.static String replaceChars(String str, String searchChars, String replaceChars)
Replaces multiple characters in a String in one go.static String replaceEach(String text, String[] searchList, String[] replacementList)
Replaces all occurrences of Strings within another String.static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)
Replaces all occurrences of Strings within another String.static String replaceOnce(String text, String searchString, String replacement)
Replaces a String with another String inside a larger String, once.static String replacePattern(String source, String regex, String replacement)
Replaces each substring of the source String that matches the given regular expression with the given replacement using the Pattern.DOTALL option.