StringBuilder Flashcards
The method that changes the length of a StringBuilder object…
public void setLength(int newLength)
Sets the length of the character sequence.
public class TestClass{ public static void main(String args[ ] ){ StringBuilder sb = new StringBuilder("12345678"); sb.setLength(5); sb.setLength(10); System.out.println(sb.length()); // will print 10 } }
substring methods..
public String substring(int begin Index)
public String substring(int start, int end)
String s = "blooper"; StringBuilder sb = new StringBuilder(s); sb.append( s.substring(4)).delete(3, 5); System.out.println(sb); // out bloerper
StringBuilder has 4 constructors ..
There are four ways to construct a StringBuilder:
new StringBuilder() - Constructs a string builder with no characters in it and an initial capacity of 16 characters.
StringBuilder(CharSequence seq) - 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.
StringBuilder(String str) - Constructs a string builder initialized to the contents of the specified string.
Same methods in StringBuilder as in String…
charAt(), indexOf(), length(), and substring()
charAt(int index)
Returns the char value in this sequence at the specified index.
length()
Returns the length (character count).
indexOf(String str, int from Index)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring
insert() method
The insert() method adds characters to the StringBuilder at the requested index and returns a reference to the current StringBuilder.
insert(int offset, String str)
insert(int offset, (almost all data types))
Inserts the string into this character sequence.but something to see after
Offset behaves exactly as an index in this case, however it’s different…
Offset
In computer science, offset describes the location of a piece of data compared to another location. For example, when a program is accessing an array of bytes, the fifth byte is offset from the beginning of the array by four bytes. If the array contained data that is meant to be read 32 bits at a time, element n would be offset from the start of the array by 4 * (n - 1) bytes.
delete methods..
delete() and deleteCharAt()
StringBuilder delete(int start, int end) StringBuilder deleteCharAt(int index)
It removes characters from the sequence and returns a reference to the current StringBuilder.
reverse a string in StringBuilder
reverse()
Reverses the chars of the string
equality with StringBuilder..
StringBuilder one = new StringBuilder(); StringBuilder two = new StringBuilder(); StringBuilder three = one.append("a"); System.out.println(one == two); // false System.out.println(one == three); // true , both references point to the same object.
StringBuilder methods inherited from Object…
clone, equals, finalize, getClass, hashCode, notify, ….
StringBuilder methods for the exam..
charAt(), indexOf(), length(), substring() append() insert() delete() deleteCharAt() reverse() toString()
public void ensureCapacity(int minimumCapacity)
public int capacity()
public void setLength(int newLength)
Method that returns the current capacity.
public int capacity()
Returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.
Method ensures that the capacity is at least equal to the specified minimum.
public void ensureCapacity(int minimumCapacity)
Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:
public void setLength(int newLength)
Sets the length of the character sequence
public void setLength(int newLength)
Sets the length of the character sequence.
If the parameter is greater than the length of the string, empty spaces are added to the string. str.length() will return the number of chars passed as newLength on the method parameter.