Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 6 February 2017

Methods of StringBuffer Class in Java

Previously we have discussed about StringBuffer class , here we will discuss all the methods of StringBuffer class with description of how to use and where to use in java program with example shown below.

Modifier and Method Name
Description
StringBuffer append(boolean b)
Appends the string representation of the boolean argument to the sequence.
StringBuffer append(char c)
Appends the string representation of the char argument to this sequence.
StringBuffer append(char[] str)
Appends the string representation of the char array argument to this sequence.
StringBuffer append(char[] str, int offset, int len)
Appends the string representation of a subarray of the char array argument to this sequence.
StringBuffer append(CharSequence s)
Appends the specified CharSequence to this sequence.
StringBuffer append(CharSequence s, int start, int end)
Appends a subsequence of the specified CharSequence to this sequence.
StringBuffer append(double d)
Appends the string representation of the double argument to this sequence.
StringBuffer append(float f)
Appends the string representation of the float argument to this sequence.
StringBuffer append(int i)
Appends the string representation of the int argument to this sequence.
StringBuffer append(long lng)
Appends the string representation of the long argument to this sequence.
StringBuffer append(Object obj)
Appends the string representation of the Object argument.
StringBuffer append(String str)
Appends the specified string to this character sequence.
StringBuffer append(StringBuffer sb)
Appends the specified StringBuffer to this sequence.
StringBuffer appendCodePoint(int codePoint)
Appends the string representation of the codePoint argument to this sequence.
int capacity()
Returns the current capacity.
char charAt(int index)
Returns the char value in this sequence at the specified index.
int codePointAt(int index)
Returns the character (Unicode code point) at the specified index.
int codePointBefore(int index)
Returns the character (Unicode code point) before the specified index.
int codePointCount(int beginIndex, int endIndex)
Returns the number of Unicode code points in the specified text range of this sequence.
StringBuffer delete(int start, int end)
Removes the characters in a substring of this sequence.
StringBuffer deleteCharAt(int index)
Removes the char at the specified position in this sequence.
void ensureCapacity(int minimumCapacity)
Ensures that the capacity is at least equal to the specified minimum.
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Characters are copied from this sequence into the destination character array dst.
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
StringBuffer insert(int offset, boolean b)
Inserts the string representation of the boolean argument into this sequence.
StringBuffer insert(int offset, char c)
Inserts the string representation of the char argument into this sequence.
StringBuffer insert(int offset, char[] str)
Inserts the string representation of the char array argument into this sequence.
StringBuffer insert(int index, char[] str, int offset, int len)
Inserts the string representation of a subarray of the str array argument into this sequence.
StringBuffer insert(int dstOffset, CharSequence s)
Inserts the specified CharSequence into this sequence.
StringBuffer insert(int dstOffset, CharSequence s, int start, int end)
Inserts a subsequence of the specified CharSequence into this sequence.
StringBuffer insert(int offset, double d)
Inserts the string representation of the double argument into this sequence.
StringBuffer insert(int offset, float f)
Inserts the string representation of the float argument into this sequence.
StringBuffer insert(int offset, int i)
Inserts the string representation of the second int argument into this sequence.
StringBuffer insert(int offset, long l)
Inserts the string representation of the long argument into this sequence.
StringBuffer insert(int offset, Object obj)
Inserts the string representation of the Object argument into this character sequence.
StringBuffer insert(int offset, String str)
Inserts the string into this character sequence.
int lastIndexOf(String str)
Returns the index within this string of the rightmost occurrence of the specified substring.
int lastIndexOf(String str, int fromIndex)
Returns the index within this string of the last occurrence of the specified substring.
int length()
Returns the length (character count).
int offsetByCodePoints(int index, int codePointOffset)
Returns the index within this sequence that is offset from the given index by codePointOffset code points.
StringBuffer replace(int start, int end, String str)
Replaces the characters in a substring of this sequence with characters in the specified String.
StringBuffer reverse()
Causes this character sequence to be replaced by the reverse of the sequence.
void setCharAt(int index, char ch)
The character at the specified index is set to ch.
void setLength(int newLength)
Sets the length of the character sequence.
CharSequence subSequence(int start, int end)
Returns a new character sequence that is a subsequence of this sequence.
String substring(int start)
Returns a new String that contains a subsequence of characters currently contained in this character sequence.
String substring(int start, int end)
Returns a new String that contains a subsequence of characters currently contained in this sequence.
String toString()
Returns a string representing the data in this sequence.
void trimToSize()
Attempts to reduce storage used for the character sequence.

Example with all Methods of StringBuffer Class:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
public class StringBufferClassMethodsExample {
 
 public static void main(String[] args) {
  
  StringBuffer sb = new StringBuffer("Java ");
  
  //Appends the string representation of the boolean argument to the sequence.
  sb.append(true);
  System.out.println("append(boolean b) : "+sb);
  
  //Appends the string representation of the char argument to this sequence.
  sb.append('I');
  System.out.println("append(char c) : "+sb);
  
  //Appends the string representation of the char array argument to this sequence.
  char[] arr = {'J','A','V','A'};
  sb.append(arr);
  System.out.println("append(char[] str) : "+sb);
  
  //Appends the string representation of a subarray of the char array argument to this sequence.
  sb.append(arr,1,2);
  System.out.println("append(char[] str, int offset, int len) : "+sb);
  
  //Appends the string representation of the double argument to this sequence.
  sb.append(5.5d);
  System.out.println("append(double d) : "+sb);
  
  //Appends the string representation of the float argument to this sequence.
  sb.append(9.55f);
  System.out.println("append(float f) : "+sb);
  
  //Appends the string representation of the int argument to this sequence.
  sb.append(10);
  System.out.println("append(int i) : "+sb);
  
  //Appends the string representation of the long argument to this sequence.
  sb.append(Long.MAX_VALUE);
  System.out.println("append(long l) : "+sb);
  
  //Appends the string representation of the Object argument.
  Object ob = new String("Identifiers");
  sb.append(ob);
  System.out.println("append(Object ob) : "+sb);
  
  //Appends the specified string to this character sequence.
  sb.append(".com");
  System.out.println("append(String s) : "+sb);
  
  //Appends the specified StringBuffer to this sequence.
  sb.append(new StringBuffer("String-Buffer"));
  System.out.println("append(StringBuffer sb) : "+sb);
  
  //Appends the string representation of the codePoint argument to this sequence.
  sb.appendCodePoint(3);
  System.out.println("appendCodePoint(int codePoint) : "+sb);
  
  //Returns the current capacity.
  System.out.println("capacity() : "+sb.capacity());
  
  //Returns the char value in this sequence at the specified index.
  System.out.println("charAt(int index) : "+sb.charAt(8));
  
  //Returns the character (Unicode code point) at the specified index.
  System.out.println("codePointAt(int index) : "+sb.codePointAt(8));
  
  //Returns the character (Unicode code point) before the specified index.
  System.out.println("codePointBefore(int index) : "+sb.codePointBefore(8));
  
  //Returns the number of Unicode code points in the specified text range of this sequence.
  System.out.println("codePointCount(int beginIndex, int endIndex) : "+sb.codePointCount(5, 20));
  
  //Removes the characters in a substring of this sequence.
  System.out.println("delete(int start, int end) : "+sb.delete(4, 59));
  
  //Removes the char at the specified position in this sequence.
  System.out.println("deleteCharAt(int index) : "+sb.deleteCharAt(17));
  
  //Ensures that the capacity is at least equal to the specified minimum.
  sb.ensureCapacity(10);
  System.out.println("ensureCapacity(int minimumCapacity) : "+sb);
  
  //Characters are copied from this sequence into the destination character array dst.
  char[] out= new char[5];
  sb.getChars(1, 5, out, 0);
  System.out.print("getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) : ");
  for(char c : out){
   System.out.print(c);
  }
  
  //Returns the index within this string of the first occurrence of the specified substring.
  System.out.println("\nindexOf(String str) : "+sb.indexOf("String"));
  
  //Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
  System.out.println("indexOf(String str, int fromIndex) : "+sb.indexOf("String", 5));
  
  //Inserts the string representation of the boolean argument into this sequence.
  System.out.println("insert(int offset, boolean b) : "+sb.insert(17, false));
  
  //Inserts the string representation of the char argument into this sequence.
  System.out.println("insert(int offset, char c)  : "+sb.insert(17, 'I'));
  
  //Inserts the string representation of the char array argument into this sequence.
  System.out.println("insert(int offset, char[] str) : "+sb.insert(17, out));
  
  //Inserts the string representation of the double argument into this sequence.
  System.out.println("insert(int offset, double d) : "+sb.insert(17,3.33d));
  
  //Inserts the string representation of the float argument into this sequence
  System.out.println("insert(int offset, float f) : "+sb.insert(0, 1.0101f));
  
  //Inserts the string representation of the second int argument into this sequence.
  System.out.println("insert(int offset, int i) : "+sb.insert(5, 101));
  
  //Inserts the string representation of the long argument into this sequence.
  System.out.println("insert(int offset, long l) : "+sb.insert(10, Long.MAX_VALUE));
  
  //Inserts the string representation of the Object argument into this character sequence.
  System.out.println("insert(int offset, Object obj) : "+sb.insert(sb.length(), new String(" LEARN MORE")));
  
  //Inserts the string into this character sequence.
  System.out.println("insert(int offset, String str) : "+sb.insert(0, "JAVA"));
  
  //Returns the index within this string of the rightmost occurrence of the specified substring.
  System.out.println("lastIndexOf(String str) : "+sb.lastIndexOf("JAVA"));
  
  //Returns the index within this string of the last occurrence of the specified substring.
  System.out.println("lastIndexOf(String str, int fromIndex) : "+sb.lastIndexOf("JAVA", 0));
  
  //Returns the length (character count).
  System.out.println("length() : "+sb.length());
  
  //Returns the index within this sequence that is offset from the given index by codePointOffset code points.
  System.out.println("offsetByCodePoints(int index, int codePointOffset) : "+sb.offsetByCodePoints(5, 10));
  
  //Replaces the characters in a substring of this sequence with characters in the specified String.
  System.out.println("replace(int start, int end, String str) : "+sb.replace(4, 60, ""));
  
  //Causes this character sequence to be replaced by the reverse of the sequence.
  System.out.println("reverse() : "+sb.reverse());
  
  //The character at the specified index is set to ch.
  sb.setCharAt(10, 'Z');
  System.out.println("setCharAt(int index, char ch) : "+sb);
  
  //Sets the length of the character sequence.
  sb.setLength(80);
  System.out.println("setLength(int newLength) :"+sb);
  
  //Returns a new character sequence that is a subsequence of this sequence.
  System.out.println("subSequence(int start, int end) : "+sb.subSequence(5, 10));
  
  //Attempts to reduce storage used for the character sequence.
  sb.trimToSize();
  System.out.println("trimToSize() : "+sb);
 }
}

Output:

append(boolean b) : Java true
append(char c) : Java trueI
append(char[] str) : Java trueIJAVA
append(char[] str, int offset, int len) : Java trueIJAVAAV
append(double d) : Java trueIJAVAAV5.5
append(float f) : Java trueIJAVAAV5.59.55
append(int i) : Java trueIJAVAAV5.59.5510
append(long l) : Java trueIJAVAAV5.59.55109223372036854775807
append(Object ob) : Java trueIJAVAAV5.59.55109223372036854775807Identifiers
append(String s) : Java trueIJAVAAV5.59.55109223372036854775807Identifiers.com
append(StringBuffer sb) : Java trueIJAVAAV5.59.55109223372036854775807Identifiers.comString-Buffer
appendCodePoint(int codePoint) : Java trueIJAVAAV5.59.55109223372036854775807Identifiers.comString-Buffer
capacity() : 90
charAt(int index) : e
codePointAt(int index) : 101
codePointBefore(int index) : 117
codePointCount(int beginIndex, int endIndex) : 15
delete(int start, int end) : JavaString-Buffer
deleteCharAt(int index) : JavaString-Buffer
ensureCapacity(int minimumCapacity) : JavaString-Buffer
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) : avaS
indexOf(String str) : 4
indexOf(String str, int fromIndex) : -1
insert(int offset, boolean b) : JavaString-Bufferfalse
insert(int offset, char c)  : JavaString-BufferIfalse
insert(int offset, char[] str) : JavaString-BufferavaS Ifalse
insert(int offset, double d) : JavaString-Buffer3.33avaS Ifalse
insert(int offset, float f) : 1.0101JavaString-Buffer3.33avaS Ifalse
insert(int offset, int i) : 1.0101011JavaString-Buffer3.33avaS Ifalse
insert(int offset, long l) : 1.0101011J9223372036854775807avaString-Buffer3.33avaS Ifalse
insert(int offset, Object obj) : 1.0101011J9223372036854775807avaString-Buffer3.33avaS Ifalse
insert(int offset, String str) : JAVA1.0101011J9223372036854775807avaString-Buffer3.33avaS Ifalse
lastIndexOf(String str) : 0
lastIndexOf(String str, int fromIndex) : 0
length() : 75
offsetByCodePoints(int index, int codePointOffset) : 15
replace(int start, int end, String str) : JAVAalse LEARN MORE
reverse() : EROM NRAEL eslaAVAJ
setCharAt(int index, char ch) : EROM NRAELZeslaAVAJ
setLength(int newLength) :EROM NRAELZeslaAVAJ

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment