有一个简单的方法来删除子字符串从给定的字符串在Java?

例如:“Hello World!”,去掉“o”→“Hell Wrld!”


当前回答

这对我很有用。

String hi = "Hello World!"
String no_o = hi.replaceAll("o", "");

或者你可以用

String no_o = hi.replace("o", "");

其他回答

你可以使用StringBuffer

StringBuffer text = new StringBuffer("Hello World");
text.replace( StartIndex ,EndIndex ,String);

这对我很有用。

String hi = "Hello World!"
String no_o = hi.replaceAll("o", "");

或者你可以用

String no_o = hi.replace("o", "");

你应该看看StringBuilder/StringBuffer,它允许你删除,插入,替换指定偏移量的字符。

你也可以使用番石榴的CharMatcher。removeFrom函数。

例子:

 String s = CharMatcher.is('a').removeFrom("bazaar");
replaceAll(String regex, String replacement)

以上方法将帮助你得到答案。

String check = "Hello World";
check = check.replaceAll("o","");