且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从字符串中删除单个字符

更新时间:2022-06-09 05:21:49

您也可以使用可变的 StringBuilder 类.

You can also use the StringBuilder class which is mutable.

StringBuilder sb = new StringBuilder(inputString);

它有方法 deleteCharAt(),以及许多其他的mutator方法.

It has the method deleteCharAt(), along with many other mutator methods.

只需删除需要删除的字符,然后得到如下结果:

Just delete the characters that you need to delete and then get the result as follows:

String resultString = sb.toString();

这避免了创建不必要的字符串对象.

This avoids creation of unnecessary string objects.