Share via


StringBuffer Class

Definition

A thread-safe, mutable sequence of characters.

[Android.Runtime.Register("java/lang/StringBuffer", DoNotGenerateAcw=true)]
public sealed class StringBuffer : Java.Lang.AbstractStringBuilder, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ISerializable, Java.Lang.IComparable, System.Collections.Generic.IEnumerable<char>
[<Android.Runtime.Register("java/lang/StringBuffer", DoNotGenerateAcw=true)>]
type StringBuffer = class
    inherit AbstractStringBuilder
    interface ISerializable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface IAppendable
    interface ICharSequence
    interface seq<char>
    interface IEnumerable
    interface IComparable
Inheritance
Attributes
Implements

Remarks

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

The principal operations on a StringBuffer 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 buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point.

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

In general, if sb refers to an instance of a StringBuffer, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence), this class synchronizes only on the string buffer performing the operation, not on the source. Note that while StringBuffer is designed to be safe to use concurrently from multiple threads, if the constructor or the append or insert operation is passed a source sequence that is shared across threads, the calling code must ensure that the operation has a consistent and unchanging view of the source sequence for the duration of the operation. This could be satisfied by the caller holding a lock during the operation's call, by using an immutable source sequence, or by not sharing the source sequence across threads.

Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

Added in 1.0.

Java documentation for java.lang.StringBuffer.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

StringBuffer()

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

StringBuffer(ICharSequence)

Constructs a string buffer that contains the same characters as the specified CharSequence.

StringBuffer(Int32)

Constructs a string buffer with no characters in it and the specified initial capacity.

StringBuffer(String)

Constructs a string buffer initialized to the contents of the specified string.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass (Inherited from AbstractStringBuilder)
ThresholdType (Inherited from AbstractStringBuilder)

Methods

Append(Boolean)

Adds the string representation of the specified boolean to the end of this StringBuffer.

Append(Char)

Adds the specified character to the end of this buffer.

Append(Char[], Int32, Int32)

Adds the specified sequence of characters to the end of this buffer.

Append(Char[])

Adds the character array to the end of this buffer.

Append(Double)

Adds the string representation of the specified double to the end of this StringBuffer.

Append(ICharSequence, Int32, Int32)

Added in 1.

Append(ICharSequence)

Appends the specified CharSequence to this sequence.

Append(Int32)

Adds the string representation of the specified integer to the end of this StringBuffer.

Append(Int64)

Adds the string representation of the specified long to the end of this StringBuffer.

Append(Object)

Adds the string representation of the specified object to the end of this StringBuffer.

Append(Single)

Adds the string representation of the specified float to the end of this StringBuffer.

Append(String, Int32, Int32)
Append(String)

Adds the specified string to the end of this buffer.

Append(StringBuffer)

Appends the specified StringBuffer to this sequence.

AppendCodePoint(Int32)

Added in 1.

Capacity()

Returns the number of characters that can be held without growing.

CharAt(Int32)

Returns the character at index.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
CodePointAt(Int32)

Added in 1.

CodePointBefore(Int32)

Added in 1.

CodePointCount(Int32, Int32)

Added in 1.

CompareTo(StringBuffer)

Compares two StringBuffer instances lexicographically.

Delete(Int32, Int32)

Added in 1.

DeleteCharAt(Int32)

Added in 1.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
EnsureCapacity(Int32)

Ensures that this object has a minimum capacity available before requiring the internal buffer to be enlarged.

Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetChars(Int32, Int32, Char[], Int32)

Copies the requested sequence of characters to the char[] passed starting at idx.

GetEnumerator()
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
IndexOf(String, Int32)

Added in 1.

IndexOf(String)

Added in 1.

Insert(Int32, Boolean)

Inserts the string representation of the specified boolean into this buffer at the specified offset.

Insert(Int32, Char)

Inserts the character into this buffer at the specified offset.

Insert(Int32, Char[], Int32, Int32)

Added in 1.

Insert(Int32, Char[])

Inserts the character array into this buffer at the specified offset.

Insert(Int32, Double)

Inserts the string representation of the specified into this buffer double at the specified offset.

Insert(Int32, ICharSequence, Int32, Int32)

Added in 1.

Insert(Int32, ICharSequence)

Added in 1.

Insert(Int32, Int32)

Inserts the string representation of the specified integer into this buffer at the specified offset.

Insert(Int32, Int64)

Inserts the string representation of the specified long into this buffer at the specified offset.

Insert(Int32, Object)

Inserts the string representation of the specified object into this buffer at the specified offset.

Insert(Int32, Single)

Inserts the string representation of the specified float into this buffer at the specified offset.

Insert(Int32, String, Int32, Int32)

Added in 1.

Insert(Int32, String)

Inserts the string into this buffer at the specified offset.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
LastIndexOf(String, Int32)

Added in 1.

LastIndexOf(String)

Added in 1.

Length()

The current length.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
OffsetByCodePoints(Int32, Int32)

Added in 1.

Replace(Int32, Int32, String)

Added in 1.

Reverse()

Added in 1.

SetCharAt(Int32, Char)

Sets the character at the index.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetLength(Int32)

Sets the current length to a new value.

SubSequence(Int32, Int32)

Added in 1.

SubSequenceFormatted(Int32, Int32)

Added in 1.

Substring(Int32, Int32)

Added in 1.

Substring(Int32)

Added in 1.

ToArray<T>() (Inherited from Object)
ToString()

Returns the current String representation.

TrimToSize()

Added in 1.

UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IComparable.CompareTo(Object)
IEnumerable.GetEnumerator()
IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)
Append(IAppendable, String, Int32, Int32)
Append(IAppendable, String)
SubSequence(ICharSequence, Int32, Int32)

Applies to