This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.


java.lang
class StringBuilder

java.lang.StringBuilder
All Implemented Interfaces:
Serializable, CharSequence

Most common way to construct:

StringBuilder sb = new StringBuilder();

Based on 98 examples


public final class StringBuilder
extends AbstractStringBuilder
implements Serializable, CharSequence

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 {@link java.lang.StringBuffer} be used.


Constructor Summary

          Constructs a string builder with no characters in it and an initial capacity of 16 characters.

          Constructs a string builder that contains the same characters as the specified CharSequence.
StringBuilder(int capacity)

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

          Constructs a string builder initialized to the contents of the specified string.
 
Method Summary
 StringBuilder
append(boolean b)

          
 StringBuilder
append(char c)

          
 StringBuilder

          
 StringBuilder
append(CharSequence s, int start, int end)

          
 StringBuilder
append(char[] str)

          
 StringBuilder
append(char[] str, int offset, int len)

          
 StringBuilder
append(double d)

          
 StringBuilder
append(float f)

          
 StringBuilder
append(int i)

          
 StringBuilder
append(long lng)

          
 StringBuilder

          
 StringBuilder

          
 StringBuilder

          Appends the specified StringBuffer to this sequence.
 StringBuilder
appendCodePoint(int codePoint)

          
 StringBuilder
delete(int start, int end)

          
 StringBuilder
deleteCharAt(int index)

          
 int

          
 int
indexOf(String str, int fromIndex)

          
 StringBuilder
insert(int offset, boolean b)

          
 StringBuilder
insert(int offset, char c)

          
 StringBuilder
insert(int dstOffset, CharSequence s)

          
 StringBuilder
insert(int dstOffset, CharSequence s, int start, int end)

          
 StringBuilder
insert(int offset, char[] str)

          
 StringBuilder
insert(int index, char[] str, int offset, int len)

          
 StringBuilder
insert(int offset, double d)

          
 StringBuilder
insert(int offset, float f)

          
 StringBuilder
insert(int offset, int i)

          
 StringBuilder
insert(int offset, long l)

          
 StringBuilder
insert(int offset, Object obj)

          
 StringBuilder
insert(int offset, String str)

          
 int

          
 int
lastIndexOf(String str, int fromIndex)

          
 StringBuilder
replace(int start, int end, String str)

          
 StringBuilder

          
 String

          
 

Constructor Detail

StringBuilder

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


StringBuilder

public StringBuilder(CharSequence seq)
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.

StringBuilder

public StringBuilder(int capacity)
Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.

Parameters:
capacity - the initial capacity.

StringBuilder

public StringBuilder(String str)
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.
Method Detail

append

public StringBuilder append(boolean b)
Overrides:
append in class AbstractStringBuilder
Parameters:
b

append

public StringBuilder append(char c)
Overrides:
append in class AbstractStringBuilder
Parameters:
c

append

public StringBuilder append(CharSequence s)
Overrides:
append in class AbstractStringBuilder
Parameters:
s

append

public StringBuilder append(CharSequence s,
                            int start,
                            int end)
Overrides:
append in class AbstractStringBuilder
Parameters:
s
start
end

append

public StringBuilder append(char[] str)
Overrides:
append in class AbstractStringBuilder
Parameters:
str

append

public StringBuilder append(char[] str,
                            int offset,
                            int len)
Overrides:
append in class AbstractStringBuilder
Parameters:
str
offset
len

append

public StringBuilder append(double d)
Overrides:
append in class AbstractStringBuilder
Parameters:
d

append

public StringBuilder append(float f)
Overrides:
append in class AbstractStringBuilder
Parameters:
f

append

public StringBuilder append(int i)
Overrides:
append in class AbstractStringBuilder
Parameters:
i

append

public StringBuilder append(long lng)
Overrides:
append in class AbstractStringBuilder
Parameters:
lng

append

public StringBuilder append(Object obj)
Overrides:
append in class AbstractStringBuilder
Parameters:
obj

append

public StringBuilder append(String str)
Overrides:
append in class AbstractStringBuilder
Parameters:
str

append

public StringBuilder append(StringBuffer sb)
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.

Overrides:
append in class AbstractStringBuilder
Parameters:
sb - the StringBuffer to append.
Returns:
a reference to this object.

appendCodePoint

public StringBuilder appendCodePoint(int codePoint)
Overrides:
appendCodePoint in class AbstractStringBuilder
Parameters:
codePoint

delete

public StringBuilder delete(int start,
                            int end)
Overrides:
delete in class AbstractStringBuilder
Parameters:
start
end

deleteCharAt

public StringBuilder deleteCharAt(int index)
Overrides:
deleteCharAt in class AbstractStringBuilder
Parameters:
index

indexOf

public int indexOf(String str)
Overrides:
indexOf in class AbstractStringBuilder
Parameters:
str

indexOf

public int indexOf(String str,
                   int fromIndex)
Overrides:
indexOf in class AbstractStringBuilder
Parameters:
str
fromIndex

insert

public StringBuilder insert(int offset,
                            boolean b)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
b

insert

public StringBuilder insert(int offset,
                            char c)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
c

insert

public StringBuilder insert(int dstOffset,
                            CharSequence s)
Overrides:
insert in class AbstractStringBuilder
Parameters:
dstOffset
s

insert

public StringBuilder insert(int dstOffset,
                            CharSequence s,
                            int start,
                            int end)
Overrides:
insert in class AbstractStringBuilder
Parameters:
dstOffset
s
start
end

insert

public StringBuilder insert(int offset,
                            char[] str)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
str

insert

public StringBuilder insert(int index,
                            char[] str,
                            int offset,
                            int len)
Overrides:
insert in class AbstractStringBuilder
Parameters:
index
str
offset
len

insert

public StringBuilder insert(int offset,
                            double d)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
d

insert

public StringBuilder insert(int offset,
                            float f)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
f

insert

public StringBuilder insert(int offset,
                            int i)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
i

insert

public StringBuilder insert(int offset,
                            long l)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
l

insert

public StringBuilder insert(int offset,
                            Object obj)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
obj

insert

public StringBuilder insert(int offset,
                            String str)
Overrides:
insert in class AbstractStringBuilder
Parameters:
offset
str

lastIndexOf

public int lastIndexOf(String str)
Overrides:
lastIndexOf in class AbstractStringBuilder
Parameters:
str

lastIndexOf

public int lastIndexOf(String str,
                       int fromIndex)
Overrides:
lastIndexOf in class AbstractStringBuilder
Parameters:
str
fromIndex

replace

public StringBuilder replace(int start,
                             int end,
                             String str)
Overrides:
replace in class AbstractStringBuilder
Parameters:
start
end
str

reverse

public StringBuilder reverse()
Overrides:
reverse in class AbstractStringBuilder

toString

public String toString()
Overrides:
toString in class AbstractStringBuilder


This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.
This page displays the Jadeite version of the documention, which is derived from the offical documentation that contains this copyright notice:
Copyright 2008 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
The official Sun™ documentation can be found here at http://java.sun.com/javase/6/docs/api/.