Class StrBuilder

  • All Implemented Interfaces:

    public class StrBuilder
    extends Object
    implements Cloneable
    Commons Lang 2 is in maintenance mode. Commons Lang 3 should be used instead.
    Builds a string from constituent parts providing a more flexible and powerful API than StringBuffer.

    The main differences from StringBuffer/StringBuilder 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

The aim has been to provide an API that mimics very closely what StringBuffer provides, but with additional methods. It should be noted that some edge cases, with invalid indices or null input, have been altered - see individual methods. The biggest of these changes is that by default, null will not output the text 'null'. This can be controlled by a property, setNullText(String).

Prior to 3.0, this class implemented Cloneable but did not implement the clone method so could not be used. From 3.0 onwards it no longer implements the interface.