×
您的位置: 首页 > 编程笔记

如何清除或清空StringBuilder?(How can I clear or empty a StringBuilder?)

Java 时间:2020-06-15  查看:736   收藏

I'm using a StringBuilder in a loop and every x iterations I want to empty it and start with an empty StringBuilder, but I can't see any method similar to the .NET StringBuilder.Clear in the documentation, just the delete method which seems overly complicated.

So what is the best way to clean out a StringBuilder in Java?

解决方案

Two ways that work:

  1. Use stringBuilderObj.setLength(0).

  2. Allocate a new one with new StringBuilder() instead of clearing the buffer.

我正在使用 StringBuilder 在一个循环和每个x迭代中我想要清空它并以空的 StringBuilder 开头,但是我看不到任何类似于.NET的方法 StringBuilder.Clear 在文档中,只是删除看起来过于复杂的方法。



那么在Java中清除 StringBuilder 的最佳方法是什么?


解决方案

两种工作方式:




  1. 使用 stringBuilderObj.setLength(0)。


  2. 使用分配一个新的StringBuilder()而不是清除缓冲区。



 

0% (0)
0% (0)