1 / 18

StringBuffer Class In Java

StringBuffer Class In Java. StringBuffer class in Java. Peer class of String String represents fixed length and immutable character sequence StringBuffer allows growable and writable character sequence

Télécharger la présentation

StringBuffer Class In Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. StringBuffer Class In Java

  2. StringBuffer class in Java • Peer class of String • String represents fixed length and immutable character sequence • StringBuffer allows growable and writable character sequence • Characters in StringBuffer can be inserted/appended/added/deleted any where and the size of the StringBuffer will automatically grow/shrink to make room

  3. StringBuffer Constructors • StringBuffer() << Reserves room for 16 characters >> • StringBuffer(int size) << Explicitly sets the size of buffer >> • StringBuffer(String str) << Sets the initial content of the string Buffer and allocates room for 16 more characters>> << When no specific length is specified , StringBuffer allocates room for 16 more characters>>

  4. int length()int capacity() • int length() returns the current length of string buffer • int capacity() returns the allocated capacity • Example : StringBuffer strbuf = new StringBuffer(); System.out.println(strbuf.length()); System.out.println(strbuf.capacity()); StringBuffer strbuf1 = new StringBuffer("Object"); System.out.println(strbuf1.length()); System.out.println(strbuf1.capacity()); 0 16 6 22

  5. void ensureCapacity(int capacity) • Useful if you know in advance the size of buffer • Used to preallocate room for a certain number of characters after StringBuffer object has been created. • <<capacity>> specifies the size of buffer StringBuffer strbuf = new StringBuffer(); System.out.println(strbuf.length()); System.out.println(strbuf.capacity()); strbuf.ensureCapacity(10); System.out.println(strbuf.capacity()); StringBuffer strbuf1 = new StringBuffer("Object"); System.out.println(strbuf1.length()); System.out.println(strbuf1.capacity()); strbuf1.ensureCapacity(20); System.out.println(strbuf1.capacity()); 0 16 16 6 22 22

  6. StringBuffer strbuf = new StringBuffer(); System.out.println(strbuf.length()); System.out.println(strbuf.capacity()); strbuf.ensureCapacity(20); System.out.println(strbuf.capacity()); StringBuffer strbuf1 = new StringBuffer("Object"); System.out.println(strbuf1.length()); System.out.println(strbuf1.capacity()); strbuf1.ensureCapacity(30); System.out.println(strbuf1.capacity()); 0 16 34 6 22 46 What will happen for the following statements strbuf.ensureCapacity(-20); strbuf1.ensureCapacity(-30); NO EFFECT ON CAPACITY

  7. StringBuffer s1 = new StringBuffer("Java"); System.out.println(s1.length()); System.out.println(s1.capacity()); s1.ensureCapacity(50); System.out.println(s1.length()); System.out.println(s1.capacity()); 4 20 4 50

  8. void setLength(int len) • Sets the length of the buffer within StringBuffer object • << len >> specifies the length of the buffer • << len >> should be non negative • If <<len>> is less than current length returned by length() then extra characters will be lost

  9. StringBuffer strbuf = new StringBuffer(); System.out.println(strbuf.length()); System.out.println(strbuf.capacity()); strbuf.setLength(20); System.out.println(strbuf.length()); System.out.println(strbuf.capacity()); StringBuffer strbuf1 = new StringBuffer("Object oriented"); System.out.println(strbuf1.length()); System.out.println(strbuf1.capacity()); strbuf1.setLength(20); System.out.println(strbuf1.length()); System.out.println(strbuf1.capacity()); StringBuffer strbuf2 = new StringBuffer("Object oriented"); System.out.println(strbuf2.length()); System.out.println(strbuf2.capacity()); strbuf2.setLength(40); System.out.println(strbuf2.length()); System.out.println(strbuf2.capacity()); 0 16 20 34 15 31 20 31 15 31 40 64

  10. char charAt(int where)void setCharAt(int where, char ch) • charAt() method is same as String class i.e returns a char from index where • setCharAt() method sets the character ch at the where index. • <<where>> should be >= 0 and should not specify a position beyond the end of the buffer StringBuffer strbuf = new StringBuffer("Object oriented"); System.out.println(strbuf); strbuf.setCharAt(10,'X'); System.out.println(strbuf); Object oriented Object oriXnted

  11. Appending [Adding at the End] String Buffer • append() method can be used for adding at the end of StringBuffer. • Append() is overloaded with following foms: • StringBuffer append(String str) • StringBuffer append(int num) • StringBuffer append(Object obj) • First Method adds String str in the end • Second converts num into string and then adds in the end • Third method calls toString() on obj and then String form of obj will be inserted in the end. [ In this case obj must supply a suitable toString() method otherwise it will be taken from Object]

  12. Append Example class Circle { private double radius; Circle(double radius) { this.radius = radius; } } class CircleTest { public static void main(String args[]) { StringBuffer strbuf = new StringBuffer("Object"); strbuf.append(" oriented"); System.out.println(strbuf); strbuf.append(6.1); System.out.println(strbuf); Object oriented Object oriented6.1

  13. Hashcode of Circle strbuf.append(6.1); System.out.println(strbuf); Circle c1 = new Circle(10.56); strbuf.append(c1); System.out.println(strbuf); } } Object oriented6.16.1 Object oriented6.16.1Circle@82ba41

  14. class Circle { private double radius; Circle(double radius) { this.radius = radius; } public String toString() { return "Circle with Radius:"+radius; } } OUTPUT JavaCircle with Radius:10.56 class CircleTest { public static void main(String args[]) { StringBuffer strbuf = new StringBuffer("Java"); Circle c1 = new Circle(10.56); strbuf.append(c1); System.out.println(strbuf); } }

  15. Inserting charcters • Insert() method can be used for inserting characters • Insert() method is also overloaded • StringBuffer insert(int index , String str); • StringBuffer insert(int index , char ch); • StringBuffer insert(int index, Object obj); • << index >> must be within permitted range and should be positive

  16. StringBuffer strbuf = new StringBuffer("Java"); strbuf.insert(3," "); System.out.println(strbuf); Jav a StringBuffer strbuf = new StringBuffer("Java"); strbuf.insert(4," "); System.out.println(strbuf); System.out.println(strbuf.length()); Java 5 StringBuffer strbuf = new StringBuffer("Java"); strbuf.insert(2,“ Programming "); System.out.println(strbuf); System.out.println(strbuf.length()); JaProgrammingva 15 StringBuffer strbuf = new StringBuffer("Java"); strbuf.insert(2,new circle(10)); System.out.println(strbuf); System.out.println(strbuf.length()); ?

  17. Reverse the String Buffer • Use reverse() method • Syntax: StringBuffer reverse(); • Examples : StringBuffer s1 = new StringBuffer(“Object”); System.out.println(s1.reverse());

  18. Deleting Characters Deleting a single charcater • To delete a single character use deleteCharAt() method • Syntax deleteCharAt() : StringBuffer deleteCharAt(int loc) << Deletes a character from index indicated by loc>> • <<loc>> should be positive and within permitted range. Deleting a Range of charcaters • To delete characters in range use delete() method • Syntax delete() : StringBuffer delete(int startIndex , int endIndex) << Deletes characters from startIndex to endIndex -1 >> • startIndex,endIndex should be positive and within permitted range and endIndex > startIndex

More Related