Skip to main content

Java String Buffer Notes

 String Buffer


1) declaration:

StringBuffer sb = new StringBuffer();


2) Functions:


(a) append(): This functions joins two strings

StringBuffer sb = new StringBuffer("Good");

sb.append("Night");

System.out.println(sb);


Output:

GoodNight


(b) insert(): This function inserts a given string at any given position(index position)


StringBuffer sb = new StringBuffer("Jva");

sb.insert(1,"a") 

System.out.println(sb);


Output:

Java


(c) replace(): This functions replaces a given string between a begining index and ending index-1


StringBuffer sb = new StringBuffer("Python Code");

sb.replace(0,5,"Java");

System.out.println(sb);


Output:

Java Code


(d) delete(): This function deletes a particular strings from the begining index and ending index-1


StringBuffer sb = new StringBuffer("Information");

sb.delete(5,11);

System.out.println(sb);

"Info"


(e) reverse(): This function reverse a particular string.


StringBuffer sb = new StringBuffer("DOG");

sb.reverse();

System.out.println(sb);

"GOD"


(f) capacity(): This function displays the length of string


StringBuffer sb = new StringBuffer();

System.out.println(sb.capacity()); // 16 is the default value

StringBuffer sb = new StringBuffer("GOD");

System.out.println(sb.capacity()); // now 3

Popular posts from this blog