Remove the Last Character of a Java StringBuilder – 删除Java StringBuilder的最后一个字符

最后修改: 2022年 8月 28日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

When we want to build a string in Java, we usually choose the convenient StringBuilder to do the job.

当我们想在Java中建立一个字符串时,我们通常会选择方便的StringBuilder来完成工作。

Suppose we have a StringBuilder sequence containing some string segments and we want to remove the last character from it. In this quick tutorial, we’ll explore three ways to do this.

假设我们有一个包含一些字符串段的StringBuilder序列,我们想删除其中的最后一个字符。在这个快速教程中,我们将探讨三种方法来做到这一点。

2. Using StringBuilder‘s deleteCharAt() Method

2.使用StringBuilderdeleteCharAt()方法

The StringBuilder class has the deleteCharAt() method. It allows us to remove the character at the desired position.

StringBuilder类有deleteCharAt()方法。它允许我们删除所需位置的字符.

The deleteCharAt() method has only one argument: the char index we want to delete.

deleteCharAt()方法只有一个参数:我们要删除的字符索引。

Therefore, if we pass the last character’s index to the method, we can remove the character. For simplicity, we’ll use unit test assertions to verify it works as expected.

因此,如果我们把最后一个字符的索引传给这个方法,我们就可以删除这个字符。为了简单起见,我们将使用单元测试断言来验证它是否按预期工作。

So next, let’s create a test to check if it works:

因此,接下来,让我们创建一个测试来检查它是否有效。

StringBuilder sb = new StringBuilder("Using the sb.deleteCharAt() method!");
sb.deleteCharAt(sb.length() - 1);
assertEquals("Using the sb.deleteCharAt() method", sb.toString());

As the test above shows, we pass the last character’s index (sb.length() -1 ) to the deleteCharAt() method, and expect the ending exclamation mark (!) to be removed.

如上面的测试所示,我们将最后一个字符的索引(sb.length() -1 )传递给deleteCharAt()方法,并期望将结尾的感叹号(!)删除。

If we run the test, it passes. So, deleteCharAt() solves the problem.

如果我们运行测试,它通过了。所以,deleteCharAt()解决了这个问题。

3. Using StringBuilder‘s replace() Method

3.使用StringBuilderreplace()方法

StringBuilder‘s replace() method allows us to replace characters in a substring of the sequence with the given string. The method accepts three parameters:

StringBuilderreplace()方法允许我们用给定的字符串替换序列的子串中的字符。该方法接受三个参数。

  • start index – the beginning index, inclusive
  • end index – the ending index, exclusive
  • replacement – the string used to replace

Let’s say the last character’s index in the sequence is lastIdx. If we want to remove the last character, we can pass lastIdx as the start index, lastIdx+1 as the end index, and an empty string “” as the replacement to replace():

比方说,序列中最后一个字符的索引是lastIdx。如果我们想删除最后一个字符,我们可以将lastIdx作为起始索引,lastIdx+1作为结束索引,并将一个空字符串“”作为替换传给replace()

StringBuilder sb = new StringBuilder("Using the sb.replace() method!");
int last = sb.length() - 1;
sb.replace(last, last + 1, "");
assertEquals("Using the sb.replace() method", sb.toString());

Now, the test above passes if we give it a run. So, the replace() method can be used to solve the problem.

现在,如果我们给它一个运行,上面的测试通过了。所以,可以用replace()方法来解决这个问题。

4. Using StringBuilder‘s substring() Method

4.使用StringBuildersubstring()方法

We can use StringBuilder‘s substring() method to get a subsequence from the given start and end indexes of the string. The method requires two arguments, the beginning index (inclusive) and the ending index (exclusive).

我们可以使用StringBuildersubstring()方法来从字符串的给定startend索引获得一个子序列。该方法需要两个参数,开始索引(包括)和结束索引(不包括)。

It’s worth mentioning that the substring() method returns a new String object. In other words, the substring() method doesn’t modify the StringBuilder object.

值得一提的是,substring()方法返回一个新的String对象。换句话说,substring()方法并不修改StringBuilder对象

We can pass 0 as the start index and the last character’s index as the end index to the substring() method to get a string with the last character chopped off:

我们可以将0作为start索引,将最后一个字符的索引作为end索引传递给substring()方法,得到一个被砍掉最后一个字符的字符串。

StringBuilder sb = new StringBuilder("Using the sb.substring() method!");
assertEquals("Using the sb.substring() method", sb.substring(0, sb.length() - 1));
//the stringBuilder object is not changed
assertEquals("Using the sb.substring() method!", sb.toString());

The test passes if we execute it.

如果我们执行它,测试就会通过。

As we can see in the test, even though the String returned by the substring() is without the last character (!), the original StringBuilder isn’t changed.

正如我们在测试中看到的,即使由 substring()返回的String没有了最后一个字符(!),原始的StringBuilder并没有改变。

5. Conclusion

5.总结

In this brief article, we’ve learned how to remove the last character from a StringBuilder sequence.

在这篇简短的文章中,我们已经学会了如何从StringBuilder序列中删除最后一个字符。

As usual, all code snippets presented here are available over on GitHub.

像往常一样,这里介绍的所有代码片段都可以在GitHub上找到