Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this
   * particular file as subject to the "Classpath" exception as provided
   * by Oracle in the LICENSE file that accompanied this code.
  *
  * This code is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * version 2 for more details (a copy is included in the LICENSE file that
  * accompanied this code).
  *
  * You should have received a copy of the GNU General Public License version
  * 2 along with this work; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  *
  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 package java.lang;


A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

For example, if z refers to a string builder object whose current contents are "start", then the method call z.append("le") would cause the string builder to contain "startle", whereas z.insert(4, "le") would alter the string builder to contain "starlet".

In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x). Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

Author(s):
Michael McCloskey
Since:
1.5
See also:
StringBuffer
String
 
 public final class StringBuilder
     extends AbstractStringBuilder
     implements java.io.SerializableCharSequence
 {

    
use serialVersionUID for interoperability
 
     static final long serialVersionUID = 4383685877147921099L;

    
Constructs a string builder with no characters in it and an initial capacity of 16 characters.
 
     public StringBuilder() {
         super(16);
     }

    
Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.

Parameters:
capacity the initial capacity.
Throws:
NegativeArraySizeException if the capacity argument is less than 0.
 
     public StringBuilder(int capacity) {
         super(capacity);
     }

    
Constructs a string builder initialized to the contents of the specified string. The initial capacity of the string builder is 16 plus the length of the string argument.

Parameters:
str the initial contents of the buffer.
Throws:
NullPointerException if str is null
    public StringBuilder(String str) {
        super(str.length() + 16);
        append(str);
    }

    
Constructs a string builder that contains the same characters as the specified CharSequence. The initial capacity of the string builder is 16 plus the length of the CharSequence argument.

Parameters:
seq the sequence to copy.
Throws:
NullPointerException if seq is null
    public StringBuilder(CharSequence seq) {
        this(seq.length() + 16);
        append(seq);
    }
    public StringBuilder append(Object obj) {
        return append(String.valueOf(obj));
    }
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }
    // Appends the specified string builder to this sequence.
    private StringBuilder append(StringBuilder sb) {
        if (sb == null)
            return append("null");
        int len = sb.length();
        int newcount =  + len;
        if (newcount > .)
            expandCapacity(newcount);
        sb.getChars(0, len);
         = newcount;
        return this;
    }

    
Appends the specified StringBuffer to this sequence.

The characters of the StringBuffer argument are appended, in order, to this sequence, increasing the length of this sequence by the length of the argument. If sb is null, then the four characters "null" are appended to this sequence.

Let n be the length of this character sequence just prior to execution of the append method. Then the character at index k in the new character sequence is equal to the character at index k in the old character sequence, if k is less than n; otherwise, it is equal to the character at index k-n in the argument sb.

Parameters:
sb the StringBuffer to append.
Returns:
a reference to this object.
    public StringBuilder append(StringBuffer sb) {
        super.append(sb);
        return this;
    }

    
    public StringBuilder append(CharSequence s) {
        if (s == null)
            s = "null";
        if (s instanceof String)
            return this.append((String)s);
        if (s instanceof StringBuffer)
            return this.append((StringBuffer)s);
        if (s instanceof StringBuilder)
            return this.append((StringBuilder)s);
        return this.append(s, 0, s.length());
    }

    
    public StringBuilder append(CharSequence sint startint end) {
        super.append(sstartend);
        return this;
    }
    public StringBuilder append(char[] str) {
        super.append(str);
        return this;
    }

    
    public StringBuilder append(char[] strint offsetint len) {
        super.append(stroffsetlen);
        return this;
    }
    public StringBuilder append(boolean b) {
        super.append(b);
        return this;
    }
    public StringBuilder append(char c) {
        super.append(c);
        return this;
    }
    public StringBuilder append(int i) {
        super.append(i);
        return this;
    }
    public StringBuilder append(long lng) {
        super.append(lng);
        return this;
    }
    public StringBuilder append(float f) {
        super.append(f);
        return this;
    }
    public StringBuilder append(double d) {
        super.append(d);
        return this;
    }

    

Since:
1.5
    public StringBuilder appendCodePoint(int codePoint) {
        super.appendCodePoint(codePoint);
        return this;
    }

    
    public StringBuilder delete(int startint end) {
        super.delete(startend);
        return this;
    }

    
    public StringBuilder deleteCharAt(int index) {
        super.deleteCharAt(index);
        return this;
    }

    
    public StringBuilder replace(int startint endString str) {
        super.replace(startendstr);
        return this;
    }

    
    public StringBuilder insert(int indexchar[] strint offset,
                                int len)
    {
        super.insert(indexstroffsetlen);
        return this;
    }

    
    public StringBuilder insert(int offsetObject obj) {
        return insert(offset, String.valueOf(obj));
    }

    
    public StringBuilder insert(int offsetString str) {
        super.insert(offsetstr);
        return this;
    }

    
    public StringBuilder insert(int offsetchar[] str) {
        super.insert(offsetstr);
        return this;
    }

    
    public StringBuilder insert(int dstOffsetCharSequence s) {
        if (s == null)
            s = "null";
        if (s instanceof String)
            return this.insert(dstOffset, (String)s);
        return this.insert(dstOffsets, 0, s.length());
    }

    
    public StringBuilder insert(int dstOffsetCharSequence s,
                                int startint end)
    {
        super.insert(dstOffsetsstartend);
        return this;
    }

    
    public StringBuilder insert(int offsetboolean b) {
        super.insert(offsetb);
        return this;
    }

    
    public StringBuilder insert(int offsetchar c) {
        super.insert(offsetc);
        return this;
    }

    
    public StringBuilder insert(int offsetint i) {
        return insert(offset, String.valueOf(i));
    }

    
    public StringBuilder insert(int offsetlong l) {
        return insert(offset, String.valueOf(l));
    }

    
    public StringBuilder insert(int offsetfloat f) {
        return insert(offset, String.valueOf(f));
    }

    
    public StringBuilder insert(int offsetdouble d) {
        return insert(offset, String.valueOf(d));
    }

    
    public int indexOf(String str) {
        return indexOf(str, 0);
    }

    
    public int indexOf(String strint fromIndex) {
        return String.indexOf(, 0, ,
                              str.toCharArray(), 0, str.length(), fromIndex);
    }

    
    public int lastIndexOf(String str) {
        return lastIndexOf(str);
    }

    
    public int lastIndexOf(String strint fromIndex) {
        return String.lastIndexOf(, 0, ,
                              str.toCharArray(), 0, str.length(), fromIndex);
    }
    public StringBuilder reverse() {
        super.reverse();
        return this;
    }
    public String toString() {
        // Create a copy, don't share the array
        return new String(, 0, );
    }

    
Save the state of the StringBuilder instance to a stream (that is, serialize it).

SerialData:
the number of characters currently stored in the string builder (int), followed by the characters in the string builder (char[]). The length of the char array may be greater than the number of characters currently stored in the string builder, in which case extra characters are ignored.
    private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException {
        s.defaultWriteObject();
        s.writeInt();
        s.writeObject();
    }

    
readObject is called to restore the state of the StringBuffer from a stream.
    private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOExceptionClassNotFoundException {
        s.defaultReadObject();
         = s.readInt();
         = (char[]) s.readObject();
    }
New to GrepCode? Check out our FAQ X