问题

有没有一种简单的方法可以从Java中的给定String中删除子字符串?

例如:"Hello World!" ,删除"o" - >"Hell Wrld!"


#1 热门回答(258 赞)

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

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

#2 热门回答(7 赞)

你可以使用StringBuffer

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

#3 热门回答(5 赞)

replace('regex', 'replacement');
replaceAll('regex', 'replacement');

在你的例子中,

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

原文链接