Advertisement



< Prev
Next >




StringBuffer Methods




Some methods of StringBuffer class


In the table below, we have given a short preview about some important methods of StringBuffer class.

Methods Description
public StringBuffer append(String str) This method appends the String at the end of StringBuffer object.
StringBuffer sb= new StringBuffer("Hello"); sb.append("There");
Modifies the StringBuffer object to HelloThere
public StringBuffer append(int i) This method appends an int at the end of StringBuffer object.
StringBuffer sb= new StringBuffer("High");
sb.append(5);
Modifies the StringBuffer object to High5
public StringBuffer append(double d)
This method appends a double at the end of StringBuffer object.
StringBuffer sb= new StringBuffer("Version");
sb.append(5.0);
Modifies the StringBuffer object to Version5.0
public StringBuffer append(float f) This method appends a float at the end of StringBuffer object..
StringBuffer sb= new StringBuffer("Version");
sb.append(5.2f);
Modifies the StringBuffer object to Version5.2
public StringBuffer append(long l) This method appends a long at the end of StringBuffer object..
StringBuffer sb= new StringBuffer("Hello"); sb.append("1000L");
Modifies the StringBuffer object to Hello1000
public StringBuffer reverse() This method reverses the string of characters in StringBuffer object.
StringBuffer sb= new StringBuffer("Blue");
sb.reverse();
Modifies the StringBuffer object to eulB



Advertisement




Methods Description
public StringBuffer delete(int startIndex, int endIndex) This method deletes the substring starting at index(startIndex)and ending at index(endIndex) in StringBuffer object.
StringBuffer sb= new StringBuffer("Blue");
sb.delete(0,2);
Modifies the StringBuffer object to ue
public StringBuffer deleteCharAt(int index) This method deletes a character at specified index in StringBuffer object.
StringBuffer sb= new StringBuffer("Blue");
sb.deleteCharAt(2);
Modifies the StringBuffer object to Ble
public StringBuffer indexOf(String str) This method returns us the index of the first occurrence of a specified substring(str) in the StringBuffer object.
StringBuffer sb= new StringBuffer("Blue");
int index = sb.indexOf("ue");
Returns the index of ue- 2
public StringBuffer lastIndexOf(String str) This method returns us the index of the last occurrence of a specified substring(str) in the StringBuffer object.
StringBuffer sb= new StringBuffer("Blueue");
int lastIndex = sb.lastIndexOf("ue");
Returns the last index of ue- 4
public StringBuffer insert(int index, String str) This method inserts a String(str) at a specfied index in the StringBuffer object.
StringBuffer sb= new StringBuffer("Blue");
sb.insert(0,"Color");
Modifies the StringBuffer object to ColorBlue
public StringBuffer insert(int index, int i) This method inserts an int variable at a specfied index in the StringBuffer object.
StringBuffer sb= new StringBuffer("Blue");
sb.insert(1,"5");
Modifies the StringBuffer object to B5lue
public StringBuffer insert(int index, double d) This method inserts a double variable at a specfied index in the StringBuffer object.
StringBuffer sb= new StringBuffer("Blue");
sb.insert(1,5.0);
Modifies the StringBuffer object to B5.0lue
public StringBuffer insert(int index, float f) This method inserts a float variable at a specfied index in the StringBuffer object.
StringBuffer sb= new StringBuffer("Blue");
sb.insert(1,5.5f);
Modifies the StringBuffer object to B5.5lue
public StringBuffer insert(int index, long l) This method inserts a long variable at a specfied index in the StringBuffer object.
StringBuffer sb= new StringBuffer("Blue");
sb.insert(1,555L);
Modifies the StringBuffer object to B555lue
public void setCharAt(int index, char ch) This method sets a character at an index to specified char(ch).
StringBuffer sb= new StringBuffer("Blue");
sb.setCharAt(1,'Z');
Modifies the StringBuffer object to BZue
public void length() This method gives us total number of character in StringBuffer Object.
StringBuffer sb= new StringBuffer("Blue");
sb.length();
Returns the length of StringBuffer object of - 4



Please share this article -





< Prev
Next >
< StringBuffer
String v/s StringBuffer >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement