- StringBuffer is a thread safe, mutable sequence of characters.
- A StringBuffer is like a String , but can be modified in the same memory location.
Constructors;
1.public StringBuffer():
- It creates empty StringBuffer object with default capacity 16 characters, it means it holds 16 empty locations.
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer();
- System.out.println(sb.capacity());
- }
- }
- OutPut:
- 16
2.public StringBuffer(int capacity):
- It creates StringBuffer object with given capacity.
- Capacity value should be positive value. means >=0.
- If we pass negative value JVM throws "java.lang.NegativeArraySizeException".
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer(12);
- System.out.println(sb.capacity());
- }
- }
- OutPut:
- 12
3.public StringBuffer(String s):
- It creates StringBuffer object characters available in passed String object and with default capacity 16 (16+String length) .
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer(new String("abc"));
- System.out.println(sb.capacity());
- }
- }
- OutPut:
- 19
4.public StringBuffer(charSequence s):
- It creates StringBuffer object characters available in passed charSequence object and with default capacity 16 (16+charSequence length) .
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- CharSequence cs = "abc";
- StringBuffer sb= new StringBuffer(cs);
- System.out.println(sb.capacity());
- }
- }
- OutPut:
- 19
Methods :
1.public StringBuffer append(XXX s);
- append(String s) methods concatenates the given String and returns updated StringBuffer object.
- There are 8 methods with same name for accepting 8 primitive data type values.
- And 3 more methods for accepting StringBuffer object , Object and String objects.
- public StringBuffer append(boolean b)
- public StringBuffer append(char c)
- public StringBuffer append(char[] str)
- public StringBuffer append(char[] str, int offset, int len)
- public StringBuffer append(double d)
- public StringBuffer append(float f)
- public StringBuffer append(int i)
- public StringBuffer append(long l)
- public StringBuffer append(Object obj)
- public StringBuffer append(StringBuffer sb)
- public StringBuffer append(String str)
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer();
- sb.append("abc");
- System.out.println(sb);
- }
- }
- OutPut:
- abc
2.public StringBuffer insert(XXX s);
- where xxx is java data types
- overloaded to take all possible java data type values
- public StringBuffer insert(int offset, boolean b)
- public StringBuffer insert(int offset, char c)
- public insert(int offset, char[] str)
- public StringBuffer insert(int index, char[] str, int offset, int len)
- public StringBuffer insert(int offset, float f)
- public StringBuffer insert(int offset, int i)
- public StringBuffer insert(int offset, long l)
- public StringBuffer insert(int offset, Object obj)
- public StringBuffer insert(int offset, String str)
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer("abc");
- sb.insert(1, "c");
- System.out.println(sb);
- }
- }
- OutPut:
- acbc
3. public StringBuffer deleteCharAt(int index):
- This method used to delete character at specified index.
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer("Java");
- sb.deleteCharAt(1);
- System.out.println(sb);
- }
- }
- OutPut:
- Jva
4. public StringBuffer delete(int start , int end):
- This method used to delete characters from specified start index to end index
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer("Java Language");
- sb.delete(1,3);
- System.out.println(sb);
- }
- }
- OutPut:
- Ja Language
5. public StringBuffer setCharAt(int index, char ch):
- This method used to insert character at specified index
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer("Java Language");
- sb.insert(0, 'S');
- System.out.println(sb);
- }
- }
- OutPut:
- SJava Language
6. public int indexOf(String str):
- This method returns index of specified sub string. if not found returns -1.
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer("Java Language");
- System.out.println(sb.indexOf("J"));
- }
- }
- OutPut:
- 0
7. public int indexOf(String str, int index):
- This method returns index of specified sub string. if not found returns -1.
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer("Java Language");
- System.out.println(sb.indexOf("a",4));
- }
- }
- OutPut:
- 6
8. public int lastIndexOf(String str):
- This method returns last occurrence of of specified sub string index . if not found returns -1.
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer("Java Language");
- System.out.println(sb.lastIndexOf("a"));
- }
- }
- OutPut:
- 10
9. public int length():
- This method returns length of StringBuffer.
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer("Java Language");
- System.out.println(sb.length());
- }
- OutPut:
- 13
10. public StringBuffer replace(int start, int end, String s):
- This method replaces the characters from start index to end index with specified string.
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer("Java Language");
- System.out.println(sb.replace(0,4,"Instance of java"));
- }
- OutPut:
- Instance of java Language
11. public StringBuffer reverse():
- This method returns reverse of sequence of characters.
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer("Java Language");
- System.out.println(sb.reverse());
- }
- OutPut:
- egaugnaL avaJ
12. public String subString(int start, int end):
- This method returns sub string from start index to end index as a string.
- package com.instanceofjavaforus;
- public Class SBDemo{
- public static void main (String args[]) {
- StringBuffer sb= new StringBuffer("Java Language");
- System.out.println(sb.substring(1,3));
- }
- OutPut:
- av
No comments