Home » commons-lang-2.5-src » org.apache.commons » lang » text » [javadoc | source]
org.apache.commons.lang.text
public class: StrBuilder [javadoc | source]
java.lang.Object
   org.apache.commons.lang.text.StrBuilder

All Implemented Interfaces:
    Cloneable

Builds a string from constituent parts providing a more flexible and powerful API than StringBuffer.

The main differences from StringBuffer/StringBuilder are:

  • Views
  • 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.

    Nested Class Summary:
    class  StrBuilder.StrBuilderTokenizer  Inner class to allow StrBuilder to operate as a tokenizer. 
    class  StrBuilder.StrBuilderReader  Inner class to allow StrBuilder to operate as a writer. 
    class  StrBuilder.StrBuilderWriter  Inner class to allow StrBuilder to operate as a writer. 
    Field Summary
    static final  int CAPACITY    The extra capacity for new builders. 
    protected  char[] buffer    Internal data storage. 
    protected  int size    Current size of the buffer. 
    Constructor:
     public StrBuilder() 
     public StrBuilder(int initialCapacity) 
     public StrBuilder(String str) 
      Constructor that creates a builder from the string, allocating 32 extra characters for growth.
      Parameters:
      str - the string to copy, null treated as blank string
    Method from org.apache.commons.lang.text.StrBuilder Summary:
    append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   append,   appendAll,   appendAll,   appendAll,   appendFixedWidthPadLeft,   appendFixedWidthPadLeft,   appendFixedWidthPadRight,   appendFixedWidthPadRight,   appendNewLine,   appendNull,   appendPadding,   appendSeparator,   appendSeparator,   appendSeparator,   appendSeparator,   appendSeparator,   appendSeparator,   appendWithSeparators,   appendWithSeparators,   appendWithSeparators,   appendln,   appendln,   appendln,   appendln,   appendln,   appendln,   appendln,   appendln,   appendln,   appendln,   appendln,   appendln,   appendln,   appendln,   appendln,   asReader,   asTokenizer,   asWriter,   capacity,   charAt,   clear,   contains,   contains,   contains,   delete,   deleteAll,   deleteAll,   deleteAll,   deleteCharAt,   deleteFirst,   deleteFirst,   deleteFirst,   endsWith,   ensureCapacity,   equals,   equals,   equalsIgnoreCase,   getChars,   getChars,   getNewLineText,   getNullText,   hashCode,   indexOf,   indexOf,   indexOf,   indexOf,   indexOf,   indexOf,   insert,   insert,   insert,   insert,   insert,   insert,   insert,   insert,   insert,   insert,   isEmpty,   lastIndexOf,   lastIndexOf,   lastIndexOf,   lastIndexOf,   lastIndexOf,   lastIndexOf,   leftString,   length,   midString,   minimizeCapacity,   replace,   replace,   replaceAll,   replaceAll,   replaceAll,   replaceFirst,   replaceFirst,   replaceFirst,   reverse,   rightString,   setCharAt,   setLength,   setNewLineText,   setNullText,   size,   startsWith,   substring,   substring,   toCharArray,   toCharArray,   toString,   toStringBuffer,   trim,   validateIndex,   validateRange
    Methods from java.lang.Object:
    clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
    Method from org.apache.commons.lang.text.StrBuilder Detail:
     public StrBuilder append(Object obj) 
      Appends an object to this string builder. Appending null will call #appendNull() .
     public StrBuilder append(String str) 
      Appends a string to this string builder. Appending null will call #appendNull() .
     public StrBuilder append(StringBuffer str) 
      Appends a string buffer to this string builder. Appending null will call #appendNull() .
     public StrBuilder append(StrBuilder str) 
      Appends another string builder to this string builder. Appending null will call #appendNull() .
     public StrBuilder append(char[] chars) 
      Appends a char array to the string builder. Appending null will call #appendNull() .
     public StrBuilder append(boolean value) 
      Appends a boolean value to the string builder.
     public StrBuilder append(char ch) 
      Appends a char value to the string builder.
     public StrBuilder append(int value) 
      Appends an int value to the string builder using String.valueOf.
     public StrBuilder append(long value) 
      Appends a long value to the string builder using String.valueOf.
     public StrBuilder append(float value) 
      Appends a float value to the string builder using String.valueOf.
     public StrBuilder append(double value) 
      Appends a double value to the string builder using String.valueOf.
     public StrBuilder append(String str,
        int startIndex,
        int length) 
      Appends part of a string to this string builder. Appending null will call #appendNull() .
     public StrBuilder append(StringBuffer str,
        int startIndex,
        int length) 
      Appends part of a string buffer to this string builder. Appending null will call #appendNull() .
     public StrBuilder append(StrBuilder str,
        int startIndex,
        int length) 
      Appends part of a string builder to this string builder. Appending null will call #appendNull() .
     public StrBuilder append(char[] chars,
        int startIndex,
        int length) 
      Appends a char array to the string builder. Appending null will call #appendNull() .
     public StrBuilder appendAll(Object[] array) 
      Appends each item in an array to the builder without any separators. Appending a null array will have no effect. Each object is appended using #append(Object) .
     public StrBuilder appendAll(Collection coll) 
      Appends each item in a collection to the builder without any separators. Appending a null collection will have no effect. Each object is appended using #append(Object) .
     public StrBuilder appendAll(Iterator it) 
      Appends each item in an iterator to the builder without any separators. Appending a null iterator will have no effect. Each object is appended using #append(Object) .
     public StrBuilder appendFixedWidthPadLeft(Object obj,
        int width,
        char padChar) 
      Appends an object to the builder padding on the left to a fixed width. The toString of the object is used. If the object is larger than the length, the left hand side is lost. If the object is null, the null text value is used.
     public StrBuilder appendFixedWidthPadLeft(int value,
        int width,
        char padChar) 
      Appends an object to the builder padding on the left to a fixed width. The String.valueOf of the int value is used. If the formatted value is larger than the length, the left hand side is lost.
     public StrBuilder appendFixedWidthPadRight(Object obj,
        int width,
        char padChar) 
      Appends an object to the builder padding on the right to a fixed length. The toString of the object is used. If the object is larger than the length, the right hand side is lost. If the object is null, null text value is used.
     public StrBuilder appendFixedWidthPadRight(int value,
        int width,
        char padChar) 
      Appends an object to the builder padding on the right to a fixed length. The String.valueOf of the int value is used. If the object is larger than the length, the right hand side is lost.
     public StrBuilder appendNewLine() 
      Appends the new line string to this string builder.

      The new line string can be altered using #setNewLineText(String) . This might be used to force the output to always use Unix line endings even when on Windows.

     public StrBuilder appendNull() 
      Appends the text representing null to this string builder.
     public StrBuilder appendPadding(int length,
        char padChar) 
      Appends the pad character to the builder the specified number of times.
     public StrBuilder appendSeparator(String separator) 
      Appends a separator if the builder is currently non-empty. Appending a null separator will have no effect. The separator is appended using #append(String) .

      This method is useful for adding a separator each time around the loop except the first.

      for (Iterator it = list.iterator(); it.hasNext(); ) {
        appendSeparator(",");
        append(it.next());
      }
      
      Note that for this simple example, you should use #appendWithSeparators(Collection, String) .
     public StrBuilder appendSeparator(char separator) 
      Appends a separator if the builder is currently non-empty. The separator is appended using #append(char) .

      This method is useful for adding a separator each time around the loop except the first.

      for (Iterator it = list.iterator(); it.hasNext(); ) {
        appendSeparator(',');
        append(it.next());
      }
      
      Note that for this simple example, you should use #appendWithSeparators(Collection, String) .
     public StrBuilder appendSeparator(String standard,
        String defaultIfEmpty) 
      Appends one of both separators to the StrBuilder. If the builder is currently empty it will append the defaultIfEmpty-separator Otherwise it will append the standard-separator Appending a null separator will have no effect. The separator is appended using #append(String) .

      This method is for example useful for constructing queries

      StrBuilder whereClause = new StrBuilder();
      if(searchCommand.getPriority() != null) {
       whereClause.appendSeparator(" and", " where");
       whereClause.append(" priority = ?")
      }
      if(searchCommand.getComponent() != null) {
       whereClause.appendSeparator(" and", " where");
       whereClause.append(" component = ?")
      }
      selectClause.append(whereClause)
      
     public StrBuilder appendSeparator(char standard,
        char defaultIfEmpty) 
      Append one of both separators to the builder If the builder is currently empty it will append the defaultIfEmpty-separator Otherwise it will append the standard-separator The separator is appended using #append(char) .
     public StrBuilder appendSeparator(String separator,
        int loopIndex) 
      Appends a separator to the builder if the loop index is greater than zero. Appending a null separator will have no effect. The separator is appended using #append(String) .

      This method is useful for adding a separator each time around the loop except the first.

      for (int i = 0; i < list.size(); i++) {
        appendSeparator(",", i);
        append(list.get(i));
      }
      
      Note that for this simple example, you should use #appendWithSeparators(Collection, String) .
     public StrBuilder appendSeparator(char separator,
        int loopIndex) 
      Appends a separator to the builder if the loop index is greater than zero. The separator is appended using #append(char) .

      This method is useful for adding a separator each time around the loop except the first.

      for (int i = 0; i < list.size(); i++) {
        appendSeparator(",", i);
        append(list.get(i));
      }
      
      Note that for this simple example, you should use #appendWithSeparators(Collection, String) .
     public StrBuilder appendWithSeparators(Object[] array,
        String separator) 
      Appends an array placing separators between each value, but not before the first or after the last. Appending a null array will have no effect. Each object is appended using #append(Object) .
     public StrBuilder appendWithSeparators(Collection coll,
        String separator) 
      Appends a collection placing separators between each value, but not before the first or after the last. Appending a null collection will have no effect. Each object is appended using #append(Object) .
     public StrBuilder appendWithSeparators(Iterator it,
        String separator) 
      Appends an iterator placing separators between each value, but not before the first or after the last. Appending a null iterator will have no effect. Each object is appended using #append(Object) .
     public StrBuilder appendln(Object obj) 
      Appends an object followed by a new line to this string builder. Appending null will call #appendNull() .
     public StrBuilder appendln(String str) 
      Appends a string followed by a new line to this string builder. Appending null will call #appendNull() .
     public StrBuilder appendln(StringBuffer str) 
      Appends a string buffer followed by a new line to this string builder. Appending null will call #appendNull() .
     public StrBuilder appendln(StrBuilder str) 
      Appends another string builder followed by a new line to this string builder. Appending null will call #appendNull() .
     public StrBuilder appendln(char[] chars) 
      Appends a char array followed by a new line to the string builder. Appending null will call #appendNull() .
     public StrBuilder appendln(boolean value) 
      Appends a boolean value followed by a new line to the string builder.
     public StrBuilder appendln(char ch) 
      Appends a char value followed by a new line to the string builder.
     public StrBuilder appendln(int value) 
      Appends an int value followed by a new line to the string builder using String.valueOf.
     public StrBuilder appendln(long value) 
      Appends a long value followed by a new line to the string builder using String.valueOf.
     public StrBuilder appendln(float value) 
      Appends a float value followed by a new line to the string builder using String.valueOf.
     public StrBuilder appendln(double value) 
      Appends a double value followed by a new line to the string builder using String.valueOf.
     public StrBuilder appendln(String str,
        int startIndex,
        int length) 
      Appends part of a string followed by a new line to this string builder. Appending null will call #appendNull() .
     public StrBuilder appendln(StringBuffer str,
        int startIndex,
        int length) 
      Appends part of a string buffer followed by a new line to this string builder. Appending null will call #appendNull() .
     public StrBuilder appendln(StrBuilder str,
        int startIndex,
        int length) 
      Appends part of a string builder followed by a new line to this string builder. Appending null will call #appendNull() .
     public StrBuilder appendln(char[] chars,
        int startIndex,
        int length) 
      Appends a char array followed by a new line to the string builder. Appending null will call #appendNull() .
     public Reader asReader() 
      Gets the contents of this builder as a Reader.

      This method allows the contents of the builder to be read using any standard method that expects a Reader.

      To use, simply create a StrBuilder, populate it with data, call asReader, and then read away.

      The internal character array is shared between the builder and the reader. This allows you to append to the builder after creating the reader, and the changes will be picked up. Note however, that no synchronization occurs, so you must perform all operations with the builder and the reader in one thread.

      The returned reader supports marking, and ignores the flush method.

     public StrTokenizer asTokenizer() 
      Creates a tokenizer that can tokenize the contents of this builder.

      This method allows the contents of this builder to be tokenized. The tokenizer will be setup by default to tokenize on space, tab, newline and formfeed (as per StringTokenizer). These values can be changed on the tokenizer class, before retrieving the tokens.

      The returned tokenizer is linked to this builder. You may intermix calls to the buider and tokenizer within certain limits, however there is no synchronization. Once the tokenizer has been used once, it must be reset to pickup the latest changes in the builder. For example:

      StrBuilder b = new StrBuilder();
      b.append("a b ");
      StrTokenizer t = b.asTokenizer();
      String[] tokens1 = t.getTokenArray();  // returns a,b
      b.append("c d ");
      String[] tokens2 = t.getTokenArray();  // returns a,b (c and d ignored)
      t.reset();              // reset causes builder changes to be picked up
      String[] tokens3 = t.getTokenArray();  // returns a,b,c,d
      
      In addition to simply intermixing appends and tokenization, you can also call the set methods on the tokenizer to alter how it tokenizes. Just remember to call reset when you want to pickup builder changes.

      Calling StrTokenizer#reset(String) or StrTokenizer#reset(char[]) with a non-null value will break the link with the builder.

     public Writer asWriter() 
      Gets this builder as a Writer that can be written to.

      This method allows you to populate the contents of the builder using any standard method that takes a Writer.

      To use, simply create a StrBuilder, call asWriter, and populate away. The data is available at any time using the methods of the StrBuilder.

      The internal character array is shared between the builder and the writer. This allows you to intermix calls that append to the builder and write using the writer and the changes will be occur correctly. Note however, that no synchronization occurs, so you must perform all operations with the builder and the writer in one thread.

      The returned writer ignores the close and flush methods.

     public int capacity() 
      Gets the current size of the internal character array buffer.
     public char charAt(int index) 
      Gets the character at the specified index.
     public StrBuilder clear() 
      Clears the string builder (convenience Collections API style method).

      This method does not reduce the size of the internal character buffer. To do that, call clear() followed by #minimizeCapacity() .

      This method is the same as #setLength(int) called with zero and is provided to match the API of Collections.

     public boolean contains(char ch) 
      Checks if the string builder contains the specified char.
     public boolean contains(String str) 
      Checks if the string builder contains the specified string.
     public boolean contains(StrMatcher matcher) 
      Checks if the string builder contains a string matched using the specified matcher.

      Matchers can be used to perform advanced searching behaviour. For example you could write a matcher to search for the character 'a' followed by a number.

     public StrBuilder delete(int startIndex,
        int endIndex) 
      Deletes the characters between the two specified indices.
     public StrBuilder deleteAll(char ch) 
      Deletes the character wherever it occurs in the builder.
     public StrBuilder deleteAll(String str) 
      Deletes the string wherever it occurs in the builder.
     public StrBuilder deleteAll(StrMatcher matcher) 
      Deletes all parts of the builder that the matcher matches.

      Matchers can be used to perform advanced deletion behaviour. For example you could write a matcher to delete all occurances where the character 'a' is followed by a number.

     public StrBuilder deleteCharAt(int index) 
      Deletes the character at the specified index.
     public StrBuilder deleteFirst(char ch) 
      Deletes the character wherever it occurs in the builder.
     public StrBuilder deleteFirst(String str) 
      Deletes the string wherever it occurs in the builder.
     public StrBuilder deleteFirst(StrMatcher matcher) 
      Deletes the first match within the builder using the specified matcher.

      Matchers can be used to perform advanced deletion behaviour. For example you could write a matcher to delete where the character 'a' is followed by a number.

     public boolean endsWith(String str) 
      Checks whether this builder ends with the specified string.

      Note that this method handles null input quietly, unlike String.

     public StrBuilder ensureCapacity(int capacity) 
      Checks the capacity and ensures that it is at least the size specified.
     public boolean equals(StrBuilder other) 
      Checks the contents of this builder against another to see if they contain the same character content.
     public boolean equals(Object obj) 
      Checks the contents of this builder against another to see if they contain the same character content.
     public boolean equalsIgnoreCase(StrBuilder other) 
      Checks the contents of this builder against another to see if they contain the same character content ignoring case.
     public char[] getChars(char[] destination) 
      Copies the character array into the specified array.
     public  void getChars(int startIndex,
        int endIndex,
        char[] destination,
        int destinationIndex) 
      Copies the character array into the specified array.
     public String getNewLineText() 
      Gets the text to be appended when a new line is added.
     public String getNullText() 
      Gets the text to be appended when null is added.
     public int hashCode() 
      Gets a suitable hash code for this builder.
     public int indexOf(char ch) 
      Searches the string builder to find the first reference to the specified char.
     public int indexOf(String str) 
      Searches the string builder to find the first reference to the specified string.

      Note that a null input string will return -1, whereas the JDK throws an exception.

     public int indexOf(StrMatcher matcher) 
      Searches the string builder using the matcher to find the first match.

      Matchers can be used to perform advanced searching behaviour. For example you could write a matcher to find the character 'a' followed by a number.

     public int indexOf(char ch,
        int startIndex) 
      Searches the string builder to find the first reference to the specified char.
     public int indexOf(String str,
        int startIndex) 
      Searches the string builder to find the first reference to the specified string starting searching from the given index.

      Note that a null input string will return -1, whereas the JDK throws an exception.

     public int indexOf(StrMatcher matcher,
        int startIndex) 
      Searches the string builder using the matcher to find the first match searching from the given index.

      Matchers can be used to perform advanced searching behaviour. For example you could write a matcher to find the character 'a' followed by a number.

     public StrBuilder insert(int index,
        Object obj) 
      Inserts the string representation of an object into this builder. Inserting null will use the stored null text value.
     public StrBuilder insert(int index,
        String str) 
      Inserts the string into this builder. Inserting null will use the stored null text value.
     public StrBuilder insert(int index,
        char[] chars) 
      Inserts the character array into this builder. Inserting null will use the stored null text value.
     public StrBuilder insert(int index,
        boolean value) 
      Inserts the value into this builder.
     public StrBuilder insert(int index,
        char value) 
      Inserts the value into this builder.
     public StrBuilder insert(int index,
        int value) 
      Inserts the value into this builder.
     public StrBuilder insert(int index,
        long value) 
      Inserts the value into this builder.
     public StrBuilder insert(int index,
        float value) 
      Inserts the value into this builder.
     public StrBuilder insert(int index,
        double value) 
      Inserts the value into this builder.
     public StrBuilder insert(int index,
        char[] chars,
        int offset,
        int length) 
      Inserts part of the character array into this builder. Inserting null will use the stored null text value.
     public boolean isEmpty() 
      Checks is the string builder is empty (convenience Collections API style method).

      This method is the same as checking #length() and is provided to match the API of Collections.

     public int lastIndexOf(char ch) 
      Searches the string builder to find the last reference to the specified char.
     public int lastIndexOf(String str) 
      Searches the string builder to find the last reference to the specified string.

      Note that a null input string will return -1, whereas the JDK throws an exception.

     public int lastIndexOf(StrMatcher matcher) 
      Searches the string builder using the matcher to find the last match.

      Matchers can be used to perform advanced searching behaviour. For example you could write a matcher to find the character 'a' followed by a number.

     public int lastIndexOf(char ch,
        int startIndex) 
      Searches the string builder to find the last reference to the specified char.
     public int lastIndexOf(String str,
        int startIndex) 
      Searches the string builder to find the last reference to the specified string starting searching from the given index.

      Note that a null input string will return -1, whereas the JDK throws an exception.

     public int lastIndexOf(StrMatcher matcher,
        int startIndex) 
      Searches the string builder using the matcher to find the last match searching from the given index.

      Matchers can be used to perform advanced searching behaviour. For example you could write a matcher to find the character 'a' followed by a number.

     public String leftString(int length) 
      Extracts the leftmost characters from the string builder without throwing an exception.

      This method extracts the left length characters from the builder. If this many characters are not available, the whole builder is returned. Thus the returned string may be shorter than the length requested.

     public int length() 
      Gets the length of the string builder.
     public String midString(int index,
        int length) 
      Extracts some characters from the middle of the string builder without throwing an exception.

      This method extracts length characters from the builder at the specified index. If the index is negative it is treated as zero. If the index is greater than the builder size, it is treated as the builder size. If the length is negative, the empty string is returned. If insufficient characters are available in the builder, as much as possible is returned. Thus the returned string may be shorter than the length requested.

     public StrBuilder minimizeCapacity() 
      Minimizes the capacity to the actual length of the string.
     public StrBuilder replace(int startIndex,
        int endIndex,
        String replaceStr) 
      Replaces a portion of the string builder with another string. The length of the inserted string does not have to match the removed length.
     public StrBuilder replace(StrMatcher matcher,
        String replaceStr,
        int startIndex,
        int endIndex,
        int replaceCount) 
      Advanced search and replaces within the builder using a matcher.

      Matchers can be used to perform advanced behaviour. For example you could write a matcher to delete all occurances where the character 'a' is followed by a number.

     public StrBuilder replaceAll(char search,
        char replace) 
      Replaces the search character with the replace character throughout the builder.
     public StrBuilder replaceAll(String searchStr,
        String replaceStr) 
      Replaces the search string with the replace string throughout the builder.
     public StrBuilder replaceAll(StrMatcher matcher,
        String replaceStr) 
      Replaces all matches within the builder with the replace string.

      Matchers can be used to perform advanced replace behaviour. For example you could write a matcher to replace all occurances where the character 'a' is followed by a number.

     public StrBuilder replaceFirst(char search,
        char replace) 
      Replaces the first instance of the search character with the replace character in the builder.
     public StrBuilder replaceFirst(String searchStr,
        String replaceStr) 
      Replaces the first instance of the search string with the replace string.
     public StrBuilder replaceFirst(StrMatcher matcher,
        String replaceStr) 
      Replaces the first match within the builder with the replace string.

      Matchers can be used to perform advanced replace behaviour. For example you could write a matcher to replace where the character 'a' is followed by a number.

     public StrBuilder reverse() 
      Reverses the string builder placing each character in the opposite index.
     public String rightString(int length) 
      Extracts the rightmost characters from the string builder without throwing an exception.

      This method extracts the right length characters from the builder. If this many characters are not available, the whole builder is returned. Thus the returned string may be shorter than the length requested.

     public StrBuilder setCharAt(int index,
        char ch) 
      Sets the character at the specified index.
     public StrBuilder setLength(int length) 
      Updates the length of the builder by either dropping the last characters or adding filler of unicode zero.
     public StrBuilder setNewLineText(String newLine) 
      Sets the text to be appended when a new line is added.
     public StrBuilder setNullText(String nullText) 
      Sets the text to be appended when null is added.
     public int size() 
      Gets the length of the string builder.

      This method is the same as #length() and is provided to match the API of Collections.

     public boolean startsWith(String str) 
      Checks whether this builder starts with the specified string.

      Note that this method handles null input quietly, unlike String.

     public String substring(int start) 
      Extracts a portion of this string builder as a string.
     public String substring(int startIndex,
        int endIndex) 
      Extracts a portion of this string builder as a string.

      Note: This method treats an endIndex greater than the length of the builder as equal to the length of the builder, and continues without error, unlike StringBuffer or String.

     public char[] toCharArray() 
      Copies the builder's character array into a new character array.
     public char[] toCharArray(int startIndex,
        int endIndex) 
      Copies part of the builder's character array into a new character array.
     public String toString() 
      Gets a String version of the string builder, creating a new instance each time the method is called.

      Note that unlike StringBuffer, the string version returned is independent of the string builder.

     public StringBuffer toStringBuffer() 
      Gets a StringBuffer version of the string builder, creating a new instance each time the method is called.
     public StrBuilder trim() 
      Trims the builder by removing characters less than or equal to a space from the beginning and end.
     protected  void validateIndex(int index) 
      Validates parameters defining a single index in the builder.
     protected int validateRange(int startIndex,
        int endIndex) 
      Validates parameters defining a range of the builder.