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

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


当前回答

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

例子:

 String s = CharMatcher.is('a').removeFrom("bazaar");

其他回答

这对我很有用。

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

或者你可以用

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

如果你知道开始和结束索引,你可以使用它

string = string.substring(0, start_index) + string.substring(end_index, string.length());

你可以轻松地使用String.replace():

String helloWorld = "Hello World!";
String hellWrld = helloWorld.replace("o","");

你可以使用

String helloWorld = "Hello World";
String target = "e";
String replacement = "";
String replacedString = helloWorld.replace(target, replacement);

The answer is = Hllo World

或者你可以使用正则表达式

String original = "Java is one of best languages. OOP can be used in Java";
String regexTarget = "\\bJava\\b";
String replacedWord = original.replaceAll(regexTarget, "Python");

The answer is = Python is one of best languages. OOP can be used in Python

你可以使用StringBuffer

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