string buffnbuilder progs

4
7/25/2019 String BuffnBuilder Progs http://slidepdf.com/reader/full/string-buffnbuilder-progs 1/4 public class StringBuffProg {  /*Java StringBuffer class is used to created mutable (modifiable) string . * The StringBuffer class in java is same as String class except it is m utable i.e. it can be changed.  * * Important Constructors of StringBuffer class  1. StringBuffer(): creates an empty string buffer with the initial capaci ty of 16.  2. StringBuffer(String str): creates a string buffer with the specified s tring.  3. StringBuffer(int capacity): creates an empty string buffer with the sp ecified capacity as length.  * * * */ public static void main(String args[]){ /*  * A string that can be modified or changed is known as mutable string. * StringBuffer and StringBuilder classes are used for creating mutable string.  */ StringBuffer sb=new StringBuffer("DEfault:"); sb.append("Java");//now original string is changed System.out.println(sb);//prints DEfault:Hello Java //The insert() method inserts the given string with this string at the given position. StringBuffer sb1=new StringBuffer("Append:"); sb1.insert(1,"Java");//now original string is changed System.out.println(sb1);//prints HJavaello //The replace() method replaces the given string from the specif ied beginIndex and endIndex. StringBuffer sb2=new StringBuffer("Replace:"); sb2.replace(1,3,"Java"); System.out.println(sb2);//prints HJavalo //The delete() method of StringBuffer class deletes the string f rom the specified beginIndex to endIndex. StringBuffer sb3=new StringBuffer("Delete:"); sb3.delete(1,3); System.out.println(sb3);//prints Hlo

Upload: prekha

Post on 25-Feb-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: String BuffnBuilder Progs

7/25/2019 String BuffnBuilder Progs

http://slidepdf.com/reader/full/string-buffnbuilder-progs 1/4

public class StringBuffProg {

 /*Java StringBuffer class is used to created mutable (modifiable) string

.* The StringBuffer class in java is same as String class except it is m

utable i.e. it can be changed. ** Important Constructors of StringBuffer class

  1. StringBuffer(): creates an empty string buffer with the initial capacity of 16.  2. StringBuffer(String str): creates a string buffer with the specified string.  3. StringBuffer(int capacity): creates an empty string buffer with the specified capacity as length.

 ****/

public static void main(String args[]){

/* * A string that can be modified or changed is known as mutable

string.* StringBuffer and StringBuilder classes are used for creating

mutable string. */

StringBuffer sb=new StringBuffer("DEfault:");sb.append("Java");//now original string is changed

System.out.println(sb);//prints DEfault:Hello Java

//The insert() method inserts the given string with this stringat the given position.

StringBuffer sb1=new StringBuffer("Append:");sb1.insert(1,"Java");//now original string is changedSystem.out.println(sb1);//prints HJavaello

//The replace() method replaces the given string from the specified beginIndex and endIndex.

StringBuffer sb2=new StringBuffer("Replace:");

sb2.replace(1,3,"Java");System.out.println(sb2);//prints HJavalo

//The delete() method of StringBuffer class deletes the string from the specified beginIndex to endIndex.

StringBuffer sb3=new StringBuffer("Delete:");sb3.delete(1,3);System.out.println(sb3);//prints Hlo

Page 2: String BuffnBuilder Progs

7/25/2019 String BuffnBuilder Progs

http://slidepdf.com/reader/full/string-buffnbuilder-progs 2/4

//The reverse() method of StringBuilder class reverses the current string.

StringBuffer sb4=new StringBuffer("Reverse:");sb4.reverse();System.out.println(sb4);//prints olleH

/*The default capacity of the buffer is 16. If the number of character increases

from its current capacity, it increases the capacity by (oldcapacity*2)+2.

For example if your current capacity is 16, it will be (16*2)+2=34. */

StringBuffer sb5=new StringBuffer();System.out.println(sb5.capacity());//default 16sb5.append("Hello");System.out.println(sb5.capacity());//now 16sb5.append("java is my favourite language");System.out.println(sb5.capacity());//now (16*2)+2=34 i.e (oldcap

acity*2)+2

/*The ensureCapacity() method of StringBuffer class ensures that the given

* capacity is the minimum to the current capacity. If it is greater than the

 * current capacity, it increases the capacity by (oldcapacity*2)+2.

 * For example if your current capacity is 16, it will be (16*2)+2=34.

 ***/

StringBuffer sb6=new StringBuffer();System.out.println(sb6.capacity());//default 16sb6.append("Hello");System.out.println(sb6.capacity());//now 16sb6.append("java is my favourite language");System.out.println(sb6.capacity());//now (16*2)+2=34 i.e (oldcap

acity*2)+2sb6.ensureCapacity(10);//now no changeSystem.out.println(sb6.capacity());//now 34sb6.ensureCapacity(50);//now (34*2)+2System.out.println(sb6.capacity());//now 70

}

}

//////////////////////////////////////////////////////////////////

Page 3: String BuffnBuilder Progs

7/25/2019 String BuffnBuilder Progs

http://slidepdf.com/reader/full/string-buffnbuilder-progs 3/4

public class StringBuilProg {

/*Java StringBuilder class is used to create mutable (modifiable) string.

*The Java StringBuilder class is same as StringBuffer class except that * it is non-synchronized. It is available since JDK 1.5. ** Important Constructors of StringBuilder class

  1. StringBuilder(): creates an empty string Builder with the initial capacity of 16.  2. StringBuilder(String str): creates a string Builder with the specified string.  3. StringBuilder(int length): creates an empty string Builder with the specified capacity as length.

 **/

public static void main(String args[]){

//The StringBuilder append() method concatenates the given argum

ent with this string.

StringBuilder sb=new StringBuilder("Hello ");sb.append("Java");//now original string is changedSystem.out.println(sb);//prints Hello Java

//The StringBuilder insert() method inserts the given string with this string at the given position.

StringBuilder sb1=new StringBuilder("Hello ");sb1.insert(1,"Java");//now original string is changedSystem.out.println(sb1);//prints HJavaello

//The StringBuilder replace() method replaces the given string from the specified beginIndex and endIndex.

StringBuilder sb2=new StringBuilder("Hello");sb2.replace(1,3,"Java");System.out.println(sb2);//prints HJavalo

//The delete() method of StringBuilder class deletes the stringfrom the specified beginIndex to endIndex.

StringBuilder sb3=new StringBuilder("Hello");sb3.delete(1,3);System.out.println(sb3);//prints Hlo

//The reverse() method of StringBuilder class reverses the current string.

StringBuilder sb4=new StringBuilder("Hello");sb4.reverse();System.out.println(sb4);//prints olleH

/* * The capacity() method of StringBuilder class returns the curr

Page 4: String BuffnBuilder Progs

7/25/2019 String BuffnBuilder Progs

http://slidepdf.com/reader/full/string-buffnbuilder-progs 4/4

ent capacity * of the Builder. The default capacity of the Builder is 16.* If the number of character increases from its current capacit

y, it increases the capacity by (oldcapacity*2)+2.* For example if your current capacity is 16, it will be (16*2)

+2=34. */

StringBuilder sb5=new StringBuilder();System.out.println(sb5.capacity());//default 16sb5.append("Hello");System.out.println(sb5.capacity());//now 16sb5.append("java is my favourite language");System.out.println(sb5.capacity());//now (16*2)+2=34 i.e (oldcap

acity*2)+2

/* * The ensureCapacity() method of StringBuilder class ensures th

at the given * capacity is the minimum to the current capacity.* If it is greater than the current capacity, it increases the

 capacity by (oldcapacity*2)+2. * For example if your current capacity is 16, it will be (16*2

)+2=34.  **/

StringBuilder sb6=new StringBuilder();System.out.println(sb6.capacity());//default 16sb6.append("Hello");System.out.println(sb6.capacity());//now 16sb6.append("java is my favourite language");System.out.println(sb6.capacity());//now (16*2)+2=34 i.e (oldcap

acity*2)+2sb6.ensureCapacity(10);//now no changeSystem.out.println(sb6.capacity());//now 34

sb6.ensureCapacity(50);//now (34*2)+2System.out.println(sb6.capacity());//now 70

}}