Apache Commons 的 StrBuilder
与 Java 的 StringBuilder
有何不同?
在 Apache Commons 中,我看到 StrBuilder
已被弃用。
import org.apache.commons.lang3.text.StrBuilder;
StrBuilder sb = new StrBuilder();
我可以使用 Java 的 StringBuilder
来代替吗?
java.lang.StringBuilder
StringBuilder sb1=new StringBuilder();
最佳答案
根据我的理解,它与 Java StringBuilder 的设计非常相似,但添加了额外的功能。 From the Apache Common Lang's JavaDoc ,主要区别是:
- Not synchronized
- Not final
Subclasses have direct access to character array
Additional methods
appendWithSeparators - adds an array of values, with a separator
- appendPadding - adds a length padding characters
- appendFixedLength - adds a fixed width field to the builder
- toCharArray/getChars - simpler ways to get a range of the character array
- delete - delete char or string
- replace - search and replace for a char or string
- leftString/rightString/midString - substring without exceptions
- contains - whether the builder contains a char or string
size/clear/isEmpty - collections style API methods
Views
asTokenizer - uses the internal buffer as the source of a StrTokenizer
- asReader - uses the internal buffer as the source of a Reader
- asWriter - allows a Writer to write directly to the internal buffer
为了回答是否可以使用它,StrBuilder 在新版本中已被弃用,取而代之的是 org.apache.commons.text.TextStringBuilder。这样做是为了避免与 StringBuilder 混淆。
关于stringbuilder - Apache Common 的 StrBuilder 和 Java 的 StringBuilder 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54688811/