You are on page 1of 10

StringBuffer is a mutable sequence of characters. It is like a String but the contents of the StringBuffer can be modified after creation.

StringBuffer Capacity Every StringBuffer object has a capacity associated with it. The capacity of the StringBuffer is the number of characters it can hold. It increases automatically as more contents added to it. StringBuffers current capacity can be obtained using following method. 1. int capacity()

This method returns the current capacity of the StringBuffer object. For example, 1. StringBuffer stringBuffer = new StringBuffer(Hello World); 2. System.out.println(stringBuffer.capacity());

This will print 27 (11 + 16) on console when executed. The actual number of characters in StringBuffer can be obtained by following method. 1. int length()

Returns the actual number of characters contained in the StringBuffer object. For example, 1. StringBuffer stringBuffer = new StringBuffer(Hello World); 2. System.out.println(stringBuffer.length()); 3. System.out.println(stringBuffer.capacity());

This will print, 11 27 on console when run. Specifying initial capacity of StringBuffer Initial capacity of the StringBuffer object can be specified using following method. 1. void ensureCapacity(int initialCapacity)

Ensures that the StringBuffers initial capacity would be grater than or equal to the specified initial capacity. The new capacity of the StringBuffer is the maximum of, 1) The initialCapacity argument passed and 2) ( Old capacity * 2 ) + 2 StringBuffer Constructors StringBuffer object can be created using following StringBuffer constructors. 1) StringBuffer() Creates empty StringBuffer object having initial capacity of 16. For example, 1. StringBuffer stringBuffer = new StringBuffer();

Here, capacity of stringBuffer object would be 16. 2) StringBuffer(int capacity) Creates empty StringBuffer object having initial capacity specified. For example, 1. StringBuffer stringBuffer = new StringBuffer(50);

Here, capacity of stringBuffer object would be 50. This constructor may throw NegativeArraySizeException if the passed argument is less than 0. 3) StringBuffer(String content) Creates new StringBuffer object having contents same as the argument string object. The initial capacity of the newly created StringBuffer object will be the length of the argument string object + 16. For example, 1. String str = Hello; 2. StringBuffer stringBuffer = new StringBuffer(str);

Here, capacity of stringBuffer object would be 5 + 16 = 21.

String concatenation and StringBuffer In Java, String concatenation operator (+) is internally implemented using StringBuffer. For Example, 1. String str = Hello + World;

Would be executed as, 1. String str = new StringBuffer().append(Hello).append(World).toString();

First an empty StringBuffer will be created and then operands of the + operator would be appended using append method of the StringBuffer. Finally toString() method of the StringBuffer would be called to convert it to string object and the same will be returned. String concatenation using this approach is very expensive in nature, because it involves creation of temporary StringBuffer object. Then that temporary objects append method gets called and the resulting StringBuffer would be converted back to String using toString() method. When to use String and when StringBuffer? If there is a need to change the contents frequently, StringBuffer should be used instead of String because StringBuffer concatenation is significantly faster than String concatenation.
1. /* 2. Java StringBuffer Replace Example 3. This example shows how contents of java StringBuffer can be replaced using 4. replace method of Java StringBuffer class. 5. */ 6. 7. public class JavaStringBufferReplaceExample { 8. 9. public static void main(String[] args) { 10. 11. //Create the StringBuffer object 12. StringBuffer sb = new StringBuffer("Hello World"); 13. System.out.println("Original Text : " + sb); 14. 15. /* 16. To replace the contents of Java StringBuffer use 17. StringBuffer replace(int start, int end, String str) method. 18. It replaces the content from StringBuffer string from start index 19. to end - 1 index by the content of the String str. 20. */ 21. sb.replace(0,5,"Hi"); 22. System.out.println("Replaced Text : " + sb); 23. 24.

25. 26. 27. 28. 29. 30. 31. 32. 33.

} } /* Output would be Original Text : Hello World Replaced Text : Hi World */

1. /* 2. Java StringBuffer SubString Example 3. This example shows how to get a sub string of content of the StringBuffer using 4. substring method of Java StringBuffer class. 5. */ 6. 7. public class JavaStringBufferSubStringExample { 8. 9. public static void main(String[] args) { 10. //create StringBuffer object 11. StringBuffer sb = new StringBuffer("Java StringBuffer SubString Example"); 12. System.out.println("Original Text : " + sb); 13. 14. /* 15. SubString method is overloaded in StringBuffer class 16. 1. String substring(int start) 17. returns new String which contains sequence of characters contined in 18. StringBuffer starting from start index to StringBuffer.length() - 1 index 19. */ 20. String strPart1 = sb.substring(5); 21. System.out.println("Substring 1 : " + strPart1); 22. 23. /* 24. 2. String substring(int start, int end) 25. returns new String which contains sequence of characters contined in 26. StringBuffer starting from start index to end index 27. */ 28. String strPart2 = sb.substring(0,17); 29. System.out.println("Substring 2 : " + strPart2); 30. 31. /* Please note that both the methods can throw a StringIndexOutOfBoundsException 32. if start or end is invalid. 33. */ 34. 35. } 36. } 37. 38. /* 39. Output would be 40. Original Text : Java StringBuffer SubString Example

41. 42. 43.

Substring 1 : StringBuffer SubString Example Substring 2 : Java StringBuffer */

String reverse
1. /* 2. Java StringBuffer Reverse Example 3. This example shows how to reverse the content of the StringBuffer using 4. reverse method of Java StringBuffer class. 5. */ 6. 7. public class JavaStringBufferReverseExample { 8. 9. public static void main(String[] args) { 10. 11. //create StringBuffer object 12. StringBuffer sb = new StringBuffer("Java StringBuffer Reverse Example"); 13. System.out.println("Original StringBuffer Content : " + sb); 14. 15. //To reverse the content of the StringBuffer use reverse method 16. sb.reverse(); 17. System.out.println("Reversed StringBuffer Content : " + sb); 18. } 19. } 20. 21. /* 22. Output Would be 23. Original StringBuffer Content : Java StringBuffer Reverse Example 24. Reversed StringBuffer Content : elpmaxE esreveR reffuBgnirtS avaJ 25. */

delete remove character or clear content


1. /* 2. Java StringBuffer delete remove character or clear content Example 3. This example shows how to delete or remove a particular character or clear 4. entire content of a StringBuffer object. 5. */ 6. 7. public class JavaStringBufferDeleteExample { 8. 9. public static void main(String[] args) { 10. /* 11. Java StringBuffer class following methods to delete / remove characters 12. or claring the contents of a StringBuffer object. 13. */ 14.

15. 16.

/* StringBuffer delete(int start, int end) remove the characters from start 17. index to an end-1 index provided. 18. This method can throw a StringIndexOutOfBoundException if the start 19. index is invalid. 20. */ 21. StringBuffer sb1 = new StringBuffer("Hello World"); 22. sb1.delete(0,6); 23. System.out.println(sb1); 24. 25. /* 26. To clear contents of a StringBuffer use delete(int start, int end) method 27. in the below given way 28. */ 29. StringBuffer sb2 = new StringBuffer("Some Content"); 30. System.out.println(sb2); 31. sb2.delete(0, sb2.length()); 32. System.out.println(sb2); 33. 34. /* 35. StringBuffer deleteCharAt(int index) deletes the character at specified 36. index. 37. This method throws StringIndexOutOfBoundException if index is negative 38. or grater than or equal to the length. 39. */ 40. StringBuffer sb3 = new StringBuffer("Hello World"); 41. sb3.deleteCharAt(0); 42. System.out.println(sb3); 43. } 44. } 45. 46. /* 47. Output would be 48. 49. World 50. Some Content 51. 52. ello World 53. 54. */ insert method 1. /* 2. Java StringBuffer insert method Example 3. This example shows how a value can be inserted in to StringBuffer object. 4. */ 5. 6. public class JavaStringBufferInsertExample { 7.

8. 9. 10.

public static void main(String[] args) { /* Java StringBuffer class provides following methods to insert various 11. primitive values and objects to StringBuffer object at specified offset. 12. */ 13. 14. /* 15. StringBuffer insert(int offset, boolean b) method inserts 16. boolean to StringBuffer object at specified offset 17. */ 18. boolean b = true; 19. StringBuffer sb1 = new StringBuffer("Hello World"); 20. sb1.insert(6,b); 21. System.out.println(sb1); 22. 23. /* 24. StringBuffer insert(int offset, char c) method inserts 25. character to StringBuffer object at specified offset 26. */ 27. char c = 'Y'; 28. StringBuffer sb2 = new StringBuffer("Hello World"); 29. sb2.insert(6,c); 30. System.out.println(sb2); 31. 32. 33. /* 34. StringBuffer insert(int offset, char[] c1) method inserts 35. character array to StringBuffer object at specified offset 36. */ 37. char[] c1 = new char[] {'Y','e','s'}; 38. StringBuffer sb3 = new StringBuffer("Hello World"); 39. sb3.insert(6,c1); 40. System.out.println(sb3); 41. 42. 43. /* 44. StringBuffer insert(int offset, double d) method inserts 45. double to StringBuffer object at specified offset 46. */ 47. double d = 1.0; 48. StringBuffer sb4 = new StringBuffer("Hello World"); 49. sb4.insert(6,d); 50. System.out.println(sb4); 51. 52. 53. /* 54. StringBuffer insert(int offset, float f) method inserts 55. float to StringBuffer object at specified offset 56. */ 57. float f = 2.0f; 58. StringBuffer sb5 = new StringBuffer("Hello World"); 59. sb5.insert(6,f); 60. System.out.println(sb5); 61. 62.

63. /* 64. StringBuffer insert(int offset, int i) method inserts 65. integer to StringBuffer object at specified offset 66. */ 67. int i = 5; 68. StringBuffer sb6 = new StringBuffer("Hello World"); 69. sb6.insert(6,i); 70. System.out.println(sb6); 71. 72. 73. /* 74. StringBuffer insert(int offset, long l) method inserts 75. long to StringBuffer object at specified offset 76. */ 77. long l = 10; 78. StringBuffer sb7 = new StringBuffer("Hello World"); 79. sb7.insert(6,l); 80. System.out.println(sb7); 81. 82. /* 83. StringBuffer insert(int offset, Object obj) method inserts 84. Object to StringBuffer object at specified offset 85. */ 86. Object obj = new String("My"); 87. StringBuffer sb8 = new StringBuffer("Hello World"); 88. sb8.insert(6,obj); 89. System.out.println(sb8); 90. 91. /* 92. StringBuffer insert(int offset, String str) method inserts 93. String to StringBuffer object at specified offset 94. */ 95. String str = "New"; 96. StringBuffer sb9 = new StringBuffer("Hello World"); 97. sb9.insert(6,str); 98. System.out.println(sb9); 99. 100. /* 101. NOTE: Above all method throws StringIndexOutOfBoundsException if the 102. offset is less than 0 or grater than length of StringBuffer object. 103. */ 104. } 105. } 106. 107. /* 108. Output would be 109. 110. Hello true World 111. Hello Y World 112. Hello Yes World 113. Hello 1.0 World 114. Hello 2.0 World 115. Hello 5 World 116. Hello 10 World 117. Hello My World

118. 119. 120.

Hello New World */

Substring
1. /* 2. Java StringBuffer SubString Example 3. This example shows how to get a sub string of content of the StringBuffer using 4. substring method of Java StringBuffer class. 5. */ 6. 7. public class JavaStringBufferSubStringExample { 8. 9. public static void main(String[] args) { 10. //create StringBuffer object 11. StringBuffer sb = new StringBuffer("Java StringBuffer SubString Example"); 12. System.out.println("Original Text : " + sb); 13. 14. /* 15. SubString method is overloaded in StringBuffer class 16. 1. String substring(int start) 17. returns new String which contains sequence of characters contined in 18. StringBuffer starting from start index to StringBuffer.length() - 1 index 19. */ 20. String strPart1 = sb.substring(5); 21. System.out.println("Substring 1 : " + strPart1); 22. 23. /* 24. 2. String substring(int start, int end) 25. returns new String which contains sequence of characters contined in 26. StringBuffer starting from start index to end index 27. */ 28. String strPart2 = sb.substring(0,17); 29. System.out.println("Substring 2 : " + strPart2); 30. 31. /* Please note that both the methods can throw a StringIndexOutOfBoundsException 32. if start or end is invalid. 33. */ 34. 35. } 36. } 37. 38. /* 39. Output would be 40. Original Text : Java StringBuffer SubString Example 41. Substring 1 : StringBuffer SubString Example 42. Substring 2 : Java StringBuffer 43. */