iTSCET

iT Portal

StringBuffer class methods

StringBuffer class methods

append(String s)

eg:
StringBuffer sb = new StringBuffer(“set “);
sb.append(“point”);
System.out.println( sb ); // output is “set point”

insert(int offset, String s)

eg:
StringBuffer sb = new StringBuffer(“01234567”);
sb.insert(4, “—“);
System.out.println( sb ); // output is “0123—4567”

reverse()

eg:
StringBuffer sb = new StringBuffer(“A man a plan a canal Panama”);
sb.reverse();
System.out.println(sb); // output is “amanaP lanac a nalp a nam A”

toString()

eg:
This method returns the value of the StringBuffer object that invoked the method
call as a String.

StringBuffer sb = new StringBuffer(“test string”);
System.out.println( sb.toString() ); // output is “test string”

chained methods

result = method1().method2().method3();

eg:
String x = “abc”;
String y = x.concat(“def”).toUpperCase().replace(‘C’,’x’); System.out.println(“y = ” + y); // result is “ABxDEF”

Example program covering StringBuffer Methods :

public class StringBufferClassMethods
{
public static void main(String arguments[])
{
StringBuffer sb = new StringBuffer(“SuperStar “);
// toString()
System.out.println( sb.toString() );
// append()
sb.append(” RajiniKanth”);
System.out.println( sb );
// insert()
sb.insert(10, “*”);
System.out.println( sb );
// reverse()
sb.reverse();
System.out.println(sb);
String x = “bil”;
// chained methods
String y = x.concat(“fa”).toUpperCase().replace(‘F’,’L’);
System.out.println(“y = ” + y);

}
}

Output :

SuperStar
SuperStar RajiniKanth
SuperStar * RajiniKanth
htnaKinijaR * ratSrepuS
y = BILLA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s