可以使用 String 类的 'substring()' 方法来去掉中间一部分字符串,而不使用下标的方式。该方法接受两个参数,分别是起始位置和结束位置(不包括结束位置的字符)。以下是一个示例:

String str = 'Hello World';
String partToRemove = 'lo Wo';

int startIndex = str.indexOf(partToRemove);
int endIndex = startIndex + partToRemove.length();

String newStr = str.substring(0, startIndex) + str.substring(endIndex);
System.out.println(newStr);

输出结果为:'Held',其中 'lo Wo' 被去掉了。


原文地址: https://www.cveoy.top/t/topic/o0u7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录