You are on page 1of 16

String Buffer Class

Constructors
1. StringBuffer() Eg: StringBuffer sb1 = new StringBuffer(); Creates an empty StringBuffer with length=0, capacity=16 2. StringBuffer(int capacity) Eg: StringBuffer sb2 = new StringBuffer(100); Creates an empty StringBuffer with length=0, capacity>=100 3. StringBuffer(String str) Eg: String s = new String(Hello!!); StringBuffer sb3 = new StringBuffer(s); StringBuffer sb4 = new StringBuffer(Bye!!);

1. length()
Returns the number of characters in the string (i.e. the length of the string in the StringBuffer) General Form: int length() Example: StringBuffer sb; sb = new StringBuffer(This is a sample); int l; l = sb.length(); Result: l 16 Note: length() is a function of class StringBuffer. The class String also has its own length() function. Both are not to be confused with Array.length

2. capacity()
Returns the capacity allocated (size of buffer) to the StringBuffer General Form: int capacity() Example: StringBuffer sb; sb = new StringBuffer(This is a sample); int c; c = sb.capacity();

Result: c 32
Note: Capacity and length of a StringBuffer are different things. Capacity of a StringBuffer is by default is: length of initial string + 16

3. setLength()
Sets the length of the calling StringBuffer to a specified length value General Form: void setLength(int length) Example: StringBuffer sb; sb = new StringBuffer(This is a sample); int len1 = sb.length(); sb.setLength(20); int len2 = sb.length(); sb.setLength(5); int len3 = sb.length(); Result: len1 16 len2 20 (null characters are added at the end) len3 5 (after 5 characters, all remaining characters are deleted) Note: New length must be +ve integer

4. ensureCapacity()
Changes the allocated capacity (size of buffer) of a StringBuffer after it has been created General Form: void ensureCapacity(int new_capacity) Example: StringBuffer sb; sb = new StringBuffer(This is a sample); int c1 = sb.capacity(); sb.ensureCapacity(100); int c2 = sb.capacity(); sb.ensureCapacity(5); int c3 = sb.capacity(); Result: c1 32 c2 100 (or maybe more) c3 100 (stays at the max capacity set or maybe even more) Note: New capacity must be +ve integer

5. charAt()
Extracts a single character at a given location from the calling string General Form: char charAt(int position) Example: StringBuffer sb; sb = new StringBuffer(This is a sample); char c,d,e; c = sb.charAt(6); d = sb.charAt(-1); e = sb.charAt(20); Result: ci d will throw an out of bound exception e will throw an out of bound exception Note: Locations range from 0 to (length-1)

6. setCharAt()
Replaces a single character at given position with a new character General Form: void setCharAt(int position, char new_char) Example: StringBuffer sb,sb1,sb2; sb = new StringBuffer(This is a sample); char c = S; sb.setCharAt(8,A); sb1=sb; sb.setCharAt(10,c); sb2=sb; Result: sb1 This is A sample sb2 This is A Sample sb This is A Sample Note: The original string in the StringBuffer is changed. Location ranges from 0 to (length-1)

7. deleteCharAt()
Deletes a single character at given position in the StringBuffer General Form: StringBuffer deleteCharAt(int position) Example: StringBuffer sb,sb1,sb2; sb = new StringBuffer(This is a sample); sb.deleteCharAt(8); sb1=new StringBuffer(sb); sb2=sb.deleteCharAt(8);

Result: sb1 This is sample (a is deleted, following chars move left) sb2 This is sample ( is deleted, following chars move left) sb This is sample (Original StringBuffer is also changed)
Note: The original string in the StringBuffer is changed. Location ranges from 0 to (length-1). A modified StringBuffer object is also returned.

8. delete()
Deletes characters from a given start position to an end position of a StringBuffer General Form: StringBuffer delete(int start_position, int end_position) Example: StringBuffer sb,sb1,sb2; sb = new StringBuffer(This is a sample); sb.delete(0,5); sb1=new StringBuffer(sb); sb2=sb.delete(0,3); Result: sb1 is a sample (This is deleted, following chars move left) sb2 a sample (is is deleted, following chars move left) sb a sample (Original StringBuffer is also changed) Note: The original string in the StringBuffer is changed. Start position ranges from 0 to (length-1). End position ranges from 0 to length. A modified StringBuffer object is also returned.

9. getChars()
Extracts a substring in the form of a char[] from a StringBuffer General Form:

void getChars(int start, int end, char[] dest, int dest_start)


dest char array where substring will be stored dest_start starting location of destination char array Example: StringBuffer sb,sb1,sb2; sb = new StringBuffer(This is a sample); char c[] = new char[sb.length()]; char d[] = new char[sb.length()]; char e[] = new char[sb.length()]; sb.getChars(0,6,c,0);

sb.getChars(10,16,d,0);
sb.getChars(0,0,e,0); Result: c This i (0 to 5) d sample (10 to 15) e (blank) (0 to 0) Note: The original string in the StringBuffer is NOT changed.

10. indexOf()
Searches for a given search string within the calling StringBuffer General Form:

int indexOf(String str) Searches for str in calling StringBuffer


int indexOf(String str, int start_search_position) Search begins at start_search_position Example: StringBuffer sb; sb = new StringBuffer(This is a sample); String s = new String(a); int f1 = sb.indexOf(is); int f2 = sb.indexOf(is,4); int f3 = sb.indexOf(s); Result: f1 2 (First occurrence of is at position 2 in This) f2 5 (First occurrence of is when search begins at position 4 is the word is) f3 8 (String variable can also be used to search) Note: The original string in the StringBuffer is NOT changed.

11. substring()
Returns a String substring from the calling StringBuffer General Form:

String substring(int start_position)


String substring(int start_position, int end_position) Example: StringBuffer sb; sb = new StringBuffer(This is a sample);

String s1,s2,s3;
s1 = sb.substring(5); s2 = sb.substring(10,16); s3 = sb.substring(0,0);

Result:
s1 is a sample s2 sample s3 (blank)

Note: start_position ranges from 0 to (length-1). end_position ranges from 0 to length.


The original string in the StringBuffer is NOT changed.

12. append()
Adds a given string to the end (i.e. appends) of the calling StringBuffer General Form:

StringBuffer append(String append_string)


Example: StringBuffer sb; sb = new StringBuffer(This is a sample); String s1,s2,s3;

sample_class x = new sample_class( This is added to the end.); *


s1 = sb.append(.); s2 = new StringBuffer(sb); sb.append(x);

Result:
s1 This is a sample. This is added to the end. s2 This is a sample. sb This is a sample. This is added to the end.

Note: If append_string is of any data type other than String, it is first converted to String.

13. insert()
Inserts a string into the calling StringBuffer at the specified position. General Form:

StringBuffer insert(int position, String insert_string)


Example: StringBuffer sb; sb = new StringBuffer(This is a sample);

StringBuffer s1,s2;
s1 = sb.insert(10,new ); s2 = new StringBuffer(sb); sb.substring(sb.length(),!!!);

Result:
s1 This is a new sample!!! s2 This is a new sample sb This is a new sample!!!

Note: position ranges from 0 to length. If position = length, then the string is inserted at the end.
If insert_string is of any data type other than String, it is first converted to String.

14. replace()
Replaces a substring from the calling StringBuffer at the specified position with a given string. General Form:

StringBuffer replace(int start, int end, String replace_string)


Example: StringBuffer sb; sb = new StringBuffer(This is a sample);

StringBuffer s1,s2;
s1 = sb.replace(0,4,It); s2 = new StringBuffer(sb); sb.substring(6,sb.length(),a replacement);

Result:
s1 It is a replacement s2 It is a sample sb It is a replacement

Note: start ranges from 0 to (length-1). end ranges from 0 to length. If replace_string is of any data type other than String, it is first converted to String.

You might also like