Skip to content

Latest commit

 

History

History
125 lines (120 loc) · 4.39 KB

StringBuilder.md

File metadata and controls

125 lines (120 loc) · 4.39 KB

StringBuilder

StringBuilder objects are like String objects, except that they can be modified. Internally, these objects are treated like variable-length arrays that contain a sequence of characters. At any point, the length and content of the sequence can be changed through method invocations.

Constructor

Constructor Description
StringBuilder() Creates an empty string builder with a capacity of 16 (16 empty elements).
StringBuilder(CharSequence cs) Constructs a string builder containing the same characters as the specified CharSequence, plus an extra 16 empty elements trailing the CharSequence.
StringBuilder(int initCapacity) Creates an empty string builder with the specified initial capacity.
StringBuilder(String s) Creates a string builder whose value is initialized by the specified string, plus an extra 16 empty elements trailing the string.

Various StringBuilder Methods

Abbreviation Meaning
StringBuilder append(boolean b)
Appends the argument to this string builder.
The data is converted to a string before the append operation takes place.
StringBuilder append(char c)
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 append(Object obj)
StringBuilder append(String s)
StringBuilder delete(int start, int end) The first method deletes the subsequence from start to end-1 (inclusive) in the StringBuilder's char sequence.
The second method deletes the character located at index.
StringBuilder deleteCharAt(int index)
StringBuilder insert(int offset, boolean b) Inserts the second argument into the string builder.
The first integer argument indicates the index before which the data is to be inserted.
The data is converted to a string before the insert operation takes place.
StringBuilder insert(int offset, char c)
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 lng)
StringBuilder insert(int offset, Object obj)
StringBuilder insert(int offset, String s)
StringBuilder replace(int start, int end, String s) Replaces the specified character(s) in this string builder.
void setCharAt(int index, char c)
StringBuilder reverse() Reverses the sequence of characters in this string builder.
String toString() Returns a string that contains the character sequence in the builder.
int length() Returns the length of the characters in the String
char charAt() Returns the char value in this sequence at the specified index.
int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring.

Note:

  • You can use any String method on a StringBuilder object by first converting the string builder to a string with the toString() method of the StringBuilder class. Then convert the string back into a string builder using the StringBuilder(String str) constructor.
  • System.out.println(sb) will print the string representation the StringBuilder object