2

How is StrBuilder from Apache Commons different from Java's StringBuilder?

In Apache Commons I see StrBuilder is deprecated.

import org.apache.commons.lang3.text.StrBuilder;
StrBuilder sb = new StrBuilder();

Can I use Java's StringBuilder instead?

java.lang.StringBuilder
StringBuilder sb1=new StringBuilder();
asherbret
  • 5,439
  • 4
  • 38
  • 58
Hemanth Peela
  • 169
  • 4
  • 14

1 Answers1

2

From my understanding, it closely resembles the Java StringBuilder's design but adds additional functionality to it. From the Apache Common Lang's JavaDoc, the main differences are:

  • 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

To answer whether or not you can use it, StrBuilder was depracated in newer versions in favor of org.apache.commons.text.TextStringBuilder. This was done to avoid confusion with StringBuilder.

John Juran
  • 21
  • 2