如何清除或清空StringBuilder? [英] How can I clear or empty a StringBuilder?

查看:4180
本文介绍了如何清除或清空StringBuilder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

两种工作方式:


  1. 使用 stringBuilderObj.setLength(0)

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

  1. Use stringBuilderObj.setLength(0).
  2. Allocate a new one with new StringBuilder() instead of clearing the buffer.

这篇关于如何清除或清空StringBuilder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆