StringBuffer methods in java
StringBuffer methods in java:
As we know that StringBuffer and StringBuilder are mutable where you can edit / modify without creating the new String object /instance. StringBuffer and StringBuilder provides different methods to be done on String. The methods in StringBuffer and StringBuilder are similiar the only difference is StringBuffer is synchronized and thread safe where StringBuilder is non-synchronized and it is not thread safe.
See the Difference between StringBuffer and StringBuilder
Syntax:
int indexOf() methods:
As we know that StringBuffer and StringBuilder are mutable where you can edit / modify without creating the new String object /instance. StringBuffer and StringBuilder provides different methods to be done on String. The methods in StringBuffer and StringBuilder are similiar the only difference is StringBuffer is synchronized and thread safe where StringBuilder is non-synchronized and it is not thread safe.
See the Difference between StringBuffer and StringBuilder
Syntax:
- int length()
- int capacity()
- void ensureCapacity(int MinimumCapacity)
- void trimToSize()
- void setLength(int length)
- char charAt(int index)
- int codePointAt(int index)
- int codePointBefore(int index)
- int codePointCount(int Beginindex, int Endindex)
- int offsetByCodePoints(int index, int offsetcodepoint)
- void getChars(int srcBegin, int srcEnd, char[], int destinationBeginindex)
- void setCharAt(int index, char ch)
- StringBuffer delete(int beginIndex, int endIndex)
- StringBuffer deleteCharAt(int index)
- StringBuffer replace(int Beginindex, int Endindex, String string)
- StringBuffer insert(int offset, char[] str)
- StringBuffer insert(int index, char[] str,int offset, int length)
- StringBuffer insert(int dstIndex, charSequence str)
- StringBuffer insert(int offset, boolean b)
- StringBuffer insert(int offset, char c)
- StringBuffer insert(int offset, int i)
- StringBuffer insert(int offset, long l)
- StringBuffer insert(int offset, float f)
- StringBuffer insert(int offset, double d)
- int indexOf(String s)
- int indexOf(String s, int afterIndex)
- StringBuffer reverse()
- String toString()
- StringBuffer append(String s)
- StringBuffer append(double d)
- StringBuffer append(float f)
- StringBuffer append(long l)
- StringBuffer append(int i)
- StringBuffer append(char c)
- StringBuffer append(boolean b)
- StringBuffer append(char[] c, int offset, int length)
- StringBuffer append(char[] c)
- StringBuffer append(CharSequence c)
- StringBuffer append(CharSequence c, int start, int end)
int length() method:
This method is used to find the length of the string. It counts each character with one including the space in the string.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
System.out.println("Length before appending: " + sb.length());
sb.append("Program");
System.out.println("Length after appending: " + sb.length());
}
}
Output:
Length before appending: 5
Length after appending: 12
int capacity() method:
This method is used to find the capacity of the string. The capacity is available for the new inserted String.By default the capacity is 16, you can specified the capacity by using the
StringBuffer sb = new StringBuffer(int minimumCapacity)
If the String specified by the default constructor. i.e.., StringBuffer sb = new StringBuffer(String s). Then the capacity is calculated by
length of the content + default capacity
If the newly inserted character exceeds the capacity, then the capacity will be increased. The increased capacity is calculated by:
(old_capacity *2)+2
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
StringBuffer sb1 = new StringBuffer(10);
System.out.println("default capacity: " + sb.capacity());
System.out.println("Specified capacity: " + sb1.capacity());
StringBuffer sb2 = new StringBuffer("This is Java");
System.out.println("The capacity after exceeding it's default capacity: " + sb2.capacity());
// length of the content + default capacity
// The capacity is: 37+16 = 53
sb2.append("easiercoding is good for java");
System.out.println("After appending the capacity: " + sb2.capacity());
// after appending it content exceeding it's limit
// Now the capacity is calculated by : (old_capacity *2)+2 = (53*2)+2 = 108
}
}
Output:
default capacity: 16
Specified capacity: 10
The capacity after exceeding it's default capacity: 20
After appending the capacity: 42
void ensureCapacity(int MinimumCapacity):
This method is used to ensure that the capacity is atleast lesser than the specified capacity. If the specified ensured minimum capacity is lesser than the StringBuffer capacity then the capacity will be increased by calculation:
(oldCapacity*2)+2
If the specified ensured minimum capacity is greater or atleast equal to the StringBuffer capacity then the capacity will be remain same as old Capacity.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("java program");
System.out.println("Before ensuring the capacity: " + sb.capacity()); // 16 + 12 = 28
sb.ensureCapacity(30);
System.out.println("After ensuring the capacity: " + sb.capacity()); //(28*2)+2 = 58
System.out.println("-------------------------------");
StringBuffer sb1 = new StringBuffer("Coding is not difficult");
System.out.println("Before ensuring the capacity: " + sb1.capacity()); // 16 + 23 = 39
sb1.ensureCapacity(30);
System.out.println("After ensuring the capacity: " + sb1.capacity());
}
}
Output:
Before ensuring the capacity: 28
After ensuring the capacity: 58
-------------------------------
Before ensuring the capacity: 39
After ensuring the capacity: 39
void trimToSize() method:
This method is used to reduce the capacity/storage depending upon the length of the string content. If the buffer is larger than the specified sequence of the character, then it will reduce the storage for space efficient.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java");
sb.append(" is a programming language");
System.out.println("Before trimming the capacity with respective to content Size: " + sb.capacity());
System.out.println("The content is: " + sb);
System.out.println("The content has the length: " + sb.length());
sb.trimToSize();
System.out.println("After trimming the size respective to content: " + sb.capacity());
}
}
Output:
Before trimming the capacity with respective to content Size: 42
The content is: Java is a programming language
The content has the length: 30
After trimming the size respective to content: 30
void setLength(int length):
This method is used to set the length of the StringBuffer. If the length is less than the original length of the StringBuffer, then the contents in the StringBuffer truncated by the length we specified
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java program");
System.out.println("Length of the content: " + sb.length());
sb.setLength(10);
System.out.println("After the length is specified: " + sb.length());
System.out.println("--------------------------------");
StringBuffer sb1 = new StringBuffer("Coding is not difficult");
System.out.println("Length of the content: " + sb1.length());
sb1.setLength(25);
System.out.println("After the length is specified: " + sb1.length());
}
}
Output:
Length of the content: 12
After the length is specified: 10
--------------------------------
Length of the content: 23
After the length is specified: 25
char charAt(int index) method:
This method is used to return the character at the specified index in the StringBuffer. The index number starts from 0 to n-1.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java Programming");
System.out.println("Finding the char at the specified index: " + sb.charAt(15));
}
}
Output:
Finding the char at the specified index: g
int codePointAt(int index) method:
This method is used to find the code point at the specified index. It will returns the unicode of the specified index character in the StringBuffer.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
System.out.println("Code point at the specifed index: " + sb.codePointAt(3));
}
}
Output:
Code point at the specifed index: 97
int codePointBefore(int index) method:
This method returns the character unicode just before the specified index in the StringBuffer
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
System.out.println("Code point before the specifed index: " + sb.codePointBefore(3));
}
}
Output:
Code point before the specifed index: 118
int codePointCount(int Beginindex, int Endindex) method:
This method returns the number of unicode int the specified range of sequence. The text range begins from the specified BeginIndex and end at EndIndex. Thus the length is calculated by
EndIndex-BeginIndex
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
System.out.println("Code point counts: " + sb.codePointCount(3,7));
}
}
Output:
Code point counts: 4
int offsetByCodePoints(int index, int offsetcodepoint) method:
This method returns the index within the string that is offset from the index to the codePointOffset code point. Unpaired surrogate within the given index and offsetcodepoint count one as each.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
System.out.println("offset code point counts: " + sb.offsetByCodePoints(6,12));
}
}
Output:
offset code point counts: 18
void getChars(int srcBegin, int srcEnd, char[], int destinationBeginindex) method:
This method is used to copy the sequence of character to the subarray. The first character to be copied is at the index srcBegin to the character at the index srcEnd . The character are copied into the sub array starting from the index destinationBeginIndex.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
char[] ch = new char[7];
try
{
sb.getChars(10,17,ch,0);
System.out.print("StringBuffer getChar() method: ");
System.out.println(ch);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
Output:
StringBuffer getChar() method: program
void setCharAt(int index, char ch) method:
This method is used to set the char at the specified index with the given character (replaceable character) that is identical to old character.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
System.out.println("Before change in the character: " + sb.toString());
sb.setCharAt(10,'P');
System.out.println("After changing the character: " + sb.toString());
}
}
Output:
Before change in the character: Java is a programming language
After changing the character: Java is a Programming language
here small letter 'p' is replaced with caps 'P'
StringBuffer delete(int beginIndex, int endIndex) method:
This method is used to removes the substring of the StringBuffer that is specified at beginIndex extends endIndex.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a powerful programming language");
System.out.println("Before deleting the content: " + sb.toString());
sb.delete(10,19);
System.out.println("After deleting the content: " + sb.toString());
}
}
Output:
Before deleting the content: Java is a powerful programming language
After deleting the content: Java is a programming language
StringBuffer deleteCharAt(int index) method:
This method is used to remove the character from the sequence at the specified index. If the index exceeds the length of the StringBuffer then it leaves java.lang.StringIndexOutOfBoundsException
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a powerful programming language");
System.out.println("Before deleting the content: " + sb.toString());
sb.deleteCharAt(8);
System.out.println("After deleting the character at index: " + sb.toString());
}
}
Output:
Before deleting the content: Java is a powerful programming language
After deleting the character at index: Java is powerful programming language
StringBuffer replace(int beginIndex, int endIndex, String string) method:
This method is used to replace the sequence of character starting from beginIndex extends to endIndex with the specified string.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Hello World");
System.out.println("Before replacing: " + sb);
sb.replace(6,11,"Java");
System.out.println("After replacing: " + sb);
}
}
Output:
Before replacing: Hello World
After replacing: Hello Java
StringBuffer insert() method:
This method is used to insert the character offset to the character sequence. By inserting the character insert the length by the length of the arguments.
This methods can be overloaded to:
StringBuffer insert(int offset, char[] str)
StringBuffer insert(int index, char[] str,int offset, int length)
This method is used to set the length of the StringBuffer. If the length is less than the original length of the StringBuffer, then the contents in the StringBuffer truncated by the length we specified
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java program");
System.out.println("Length of the content: " + sb.length());
sb.setLength(10);
System.out.println("After the length is specified: " + sb.length());
System.out.println("--------------------------------");
StringBuffer sb1 = new StringBuffer("Coding is not difficult");
System.out.println("Length of the content: " + sb1.length());
sb1.setLength(25);
System.out.println("After the length is specified: " + sb1.length());
}
}
Output:
Length of the content: 12
After the length is specified: 10
--------------------------------
Length of the content: 23
After the length is specified: 25
char charAt(int index) method:
This method is used to return the character at the specified index in the StringBuffer. The index number starts from 0 to n-1.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java Programming");
System.out.println("Finding the char at the specified index: " + sb.charAt(15));
}
}
Output:
Finding the char at the specified index: g
int codePointAt(int index) method:
This method is used to find the code point at the specified index. It will returns the unicode of the specified index character in the StringBuffer.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
System.out.println("Code point at the specifed index: " + sb.codePointAt(3));
}
}
Output:
Code point at the specifed index: 97
int codePointBefore(int index) method:
This method returns the character unicode just before the specified index in the StringBuffer
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
System.out.println("Code point before the specifed index: " + sb.codePointBefore(3));
}
}
Output:
Code point before the specifed index: 118
int codePointCount(int Beginindex, int Endindex) method:
This method returns the number of unicode int the specified range of sequence. The text range begins from the specified BeginIndex and end at EndIndex. Thus the length is calculated by
EndIndex-BeginIndex
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
System.out.println("Code point counts: " + sb.codePointCount(3,7));
}
}
Output:
Code point counts: 4
int offsetByCodePoints(int index, int offsetcodepoint) method:
This method returns the index within the string that is offset from the index to the codePointOffset code point. Unpaired surrogate within the given index and offsetcodepoint count one as each.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
System.out.println("offset code point counts: " + sb.offsetByCodePoints(6,12));
}
}
Output:
offset code point counts: 18
void getChars(int srcBegin, int srcEnd, char[], int destinationBeginindex) method:
This method is used to copy the sequence of character to the subarray. The first character to be copied is at the index srcBegin to the character at the index srcEnd . The character are copied into the sub array starting from the index destinationBeginIndex.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
char[] ch = new char[7];
try
{
sb.getChars(10,17,ch,0);
System.out.print("StringBuffer getChar() method: ");
System.out.println(ch);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
Output:
StringBuffer getChar() method: program
void setCharAt(int index, char ch) method:
This method is used to set the char at the specified index with the given character (replaceable character) that is identical to old character.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a programming language");
System.out.println("Before change in the character: " + sb.toString());
sb.setCharAt(10,'P');
System.out.println("After changing the character: " + sb.toString());
}
}
Output:
Before change in the character: Java is a programming language
After changing the character: Java is a Programming language
here small letter 'p' is replaced with caps 'P'
StringBuffer delete(int beginIndex, int endIndex) method:
This method is used to removes the substring of the StringBuffer that is specified at beginIndex extends endIndex.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a powerful programming language");
System.out.println("Before deleting the content: " + sb.toString());
sb.delete(10,19);
System.out.println("After deleting the content: " + sb.toString());
}
}
Output:
Before deleting the content: Java is a powerful programming language
After deleting the content: Java is a programming language
StringBuffer deleteCharAt(int index) method:
This method is used to remove the character from the sequence at the specified index. If the index exceeds the length of the StringBuffer then it leaves java.lang.StringIndexOutOfBoundsException
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java is a powerful programming language");
System.out.println("Before deleting the content: " + sb.toString());
sb.deleteCharAt(8);
System.out.println("After deleting the character at index: " + sb.toString());
}
}
Output:
Before deleting the content: Java is a powerful programming language
After deleting the character at index: Java is powerful programming language
StringBuffer replace(int beginIndex, int endIndex, String string) method:
This method is used to replace the sequence of character starting from beginIndex extends to endIndex with the specified string.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Hello World");
System.out.println("Before replacing: " + sb);
sb.replace(6,11,"Java");
System.out.println("After replacing: " + sb);
}
}
Output:
Before replacing: Hello World
After replacing: Hello Java
StringBuffer insert() method:
This method is used to insert the character offset to the character sequence. By inserting the character insert the length by the length of the arguments.
This methods can be overloaded to:
StringBuffer insert(int offset, char[] str)
StringBuffer insert(int index, char[] str,int offset, int length)
StringBuffer insert(int dstIndex, charSequence str)
StringBuffer insert(int offset, boolean b)
StringBuffer insert(int offset, char c)
StringBuffer insert(int offset, int i)
StringBuffer insert(int offset, long l)
StringBuffer insert(int offset, float f)
StringBuffer insert(int offset, double d)
StringBuffer insert(int offset, boolean b)
StringBuffer insert(int offset, char c)
StringBuffer insert(int offset, int i)
StringBuffer insert(int offset, long l)
StringBuffer insert(int offset, float f)
StringBuffer insert(int offset, double d)
Example:
public class Example
{
public static void main(String[] args)
{
//insert(int dstIndex, charSequence str)
StringBuffer sb = new StringBuffer("Java is a programming language");
System.out.println("Before inserting the content: " + sb.toString());
sb.insert(9," powerful");
System.out.println("After inserting: " + sb.toString());
System.out.println("------------------------------");
//insert(int offset, char[] str)
StringBuffer sb1 = new StringBuffer("Java is a programming language");
char[] ch = new char[] { 'o', 'b', 'j', 'e', 'c', 't', ' ', 'o', 'r', 'i', 'e', 'n', 't', 'e', 'd', ' '};
sb1.insert(8,ch);
System.out.println("Method insert(int offset, char[] str): " + sb1.toString());
System.out.println("------------------------------");
//insert(int, char[] str, int, int)
StringBuffer sb2 = new StringBuffer("Java is programming language");
char[] c = new char[] { 'P', 'o', 'w', 'e', 'r', 'f', 'u', '1', ' '};
sb2.insert(10,c,0,9);
System.out.println("Method insert(int, char[] str, int, int): " + sb2.toString());
}
}
Output:
Before inserting the content: Java is a programming language
After inserting: Java is a powerful programming language
------------------------------
Method insert(int offset, char[] str): Java is object oriented a programming language
------------------------------
Method insert(int, char[] str, int, int): Java is prPowerfu1 ogramming language
int indexOf() methods:
This method is used to find the index of the specified substring. learn much detail about the indexOf() method here.
This methods can be overloaded as:
int indexOf(String s)
int indexOf(String s, int afterIndex)
int indexOf(String s, int afterIndex)
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("This is a java program");
int index1 = sb.indexOf("is");
int index2 = sb.indexOf("is",3);
System.out.println("index of content: " + index1);
System.out.println("index of content after index 3: " + index2);
}
}
Output:
index of content: 2
index of content after index 3: 5
StringBuffer reverse() method:
This methods is used to reverse the character of the StringBuffer. This methods does not requires any parameter.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Programming");
System.out.println("Reverse the content: " + sb.reverse());
}
}
Output:
Reverse the content: gnimmargorP
String toString() method:
This methods is used to return the string representing data from the StringBuffer object. A string method is created and initialize to get the character from the StringBuffer and returns string by toString() method.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java Programming");
System.out.println("toString() method: " + sb.toString());
}
}
Output:
toString() method: Java Programming
StringBuffer append() method:
This method is used to add/append the string representing some arguments at the end of the sequence.
There are different overloaded methods:
StringBuffer append(String s):
This method is used to append the string at the end of the sequence.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
String str = "Program";
sb.append(str);
System.out.println("Appending String: " + sb);
// you can also append the string
sb.append(" Language");
System.out.println("Appending String: " + sb);
}
}
Output:
Appending String: Java Program
Appending String: Java Program Language
StringBuffer append(double d):
This method is used to append string representing the double argument.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Value is ");
Double d = new Double(123.56);
sb.append(d);
System.out.println("Appending double argument: " + sb);
// you can also append double argument by
StringBuffer sb1 = new StringBuffer("Value changed to ");
sb1.append(567.78);
System.out.println("Appending double argument: " + sb1);
}
}
Output:
Appending double argument: Value is 123.56
Appending double argument: Value changed to 567.78
StringBuffer append(float f):
This method is used append the string representing float argument at end of the sequence
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Value is ");
Float f = new Float(3.56);
sb.append(f);
System.out.println("Appending Float argument: " + sb);
// you can also append Float argument by
StringBuffer sb1 = new StringBuffer("Value changed to ");
sb1.append(7.78);
System.out.println("Appending Float argument: " + sb1);
}
}
Output:
Appending Float argument: Value is 3.56
Appending Float argument: Value changed to 7.78
StringBuffer append(long l):
This method is used to append the string representing the long argument at end of the sequence
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Value is ");
Long l = new Long(456);
sb.append(l);
System.out.println("Appending Long argument: " + sb);
// you can also append Long argument by
StringBuffer sb1 = new StringBuffer("Value changed to ");
sb1.append(789);
System.out.println("Appending Long argument: " + sb1);
}
}
Output:
Appending Long argument: Value is 456
Appending Long argument: Value changed to 789
StringBuffer append(int i):
This method is used to append the string representing integers argument at end of the sequence.
Example:
public class String_methods
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Value is ");
Integer i = new Integer(4);
sb.append(i);
System.out.println("Appending Integer argument: " + sb);
// you can also append Integer argument by
StringBuffer sb1 = new StringBuffer("Value changed to ");
sb1.append(7);
System.out.println("Appending Integer argument: " + sb1);
}
}
Output:
Appending Integer argument: Value is 4
Appending Integer argument: Value changed to 7
StringBuffer append(char c):
This is an inbuilt method used to append the string representing character argument at the end of the sequence.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("The character is ");
sb.append('e');
System.out.println("Appending Character argument: " + sb);
}
}
Output:
Appending Character argument: The character is e
StringBuffer append(boolean b):
This is an inbuilt method used to append the string representing the boolean value at the end of the sequence
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("The value is ");
sb.append(true);
System.out.println("Appending Boolean argument: " + sb);
}
}
Output:
Appending Boolean argument: The value is true
StringBuffer append(char[] c):
This is an inbuilt method used to append the string representing the character array at the end of the sequence
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
char[] ch = new char[] {'P','r','o','g','r','a','m'};
sb.append(ch);
System.out.println("Appending character array : " + sb);
}
}
Output:
Appending character array : Java Program
StringBuffer append(char[] c, int offset, int length):
This method is used to append the string representing substring of char array to the given sequence.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
char[] ch = new char[] {'i','s',' ','P','r','o','g','r','a','m'};
sb.append(ch,3,7);
System.out.println("Appending character array : " + sb);
}
}
Output:
Appending character array : Java Program
StringBuffer append(CharSequence c):
This method is used to append the string representing the CharSequence at the end of the given sequence
.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
CharSequence ch = "program";
sb.append(ch);
System.out.println("Appending CharSequence array : " + sb);
}
}
Output:
Appending CharSequence array : Java program
StringBuffer append(CharSequence c, int start, int end):
This method is used to append the string representing the subsequence of CharSequence at the given sequence
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
CharSequence ch = "is a program";
sb.append(ch,5,12);
System.out.println("Appending CharSequence array : " + sb);
}
}
Output:
Appending CharSequence array : Java program
This method is used to append the string at the end of the sequence.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
String str = "Program";
sb.append(str);
System.out.println("Appending String: " + sb);
// you can also append the string
sb.append(" Language");
System.out.println("Appending String: " + sb);
}
}
Output:
Appending String: Java Program
Appending String: Java Program Language
StringBuffer append(double d):
This method is used to append string representing the double argument.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Value is ");
Double d = new Double(123.56);
sb.append(d);
System.out.println("Appending double argument: " + sb);
// you can also append double argument by
StringBuffer sb1 = new StringBuffer("Value changed to ");
sb1.append(567.78);
System.out.println("Appending double argument: " + sb1);
}
}
Output:
Appending double argument: Value is 123.56
Appending double argument: Value changed to 567.78
StringBuffer append(float f):
This method is used append the string representing float argument at end of the sequence
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Value is ");
Float f = new Float(3.56);
sb.append(f);
System.out.println("Appending Float argument: " + sb);
// you can also append Float argument by
StringBuffer sb1 = new StringBuffer("Value changed to ");
sb1.append(7.78);
System.out.println("Appending Float argument: " + sb1);
}
}
Output:
Appending Float argument: Value is 3.56
Appending Float argument: Value changed to 7.78
StringBuffer append(long l):
This method is used to append the string representing the long argument at end of the sequence
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Value is ");
Long l = new Long(456);
sb.append(l);
System.out.println("Appending Long argument: " + sb);
// you can also append Long argument by
StringBuffer sb1 = new StringBuffer("Value changed to ");
sb1.append(789);
System.out.println("Appending Long argument: " + sb1);
}
}
Output:
Appending Long argument: Value is 456
Appending Long argument: Value changed to 789
StringBuffer append(int i):
This method is used to append the string representing integers argument at end of the sequence.
Example:
public class String_methods
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Value is ");
Integer i = new Integer(4);
sb.append(i);
System.out.println("Appending Integer argument: " + sb);
// you can also append Integer argument by
StringBuffer sb1 = new StringBuffer("Value changed to ");
sb1.append(7);
System.out.println("Appending Integer argument: " + sb1);
}
}
Output:
Appending Integer argument: Value is 4
Appending Integer argument: Value changed to 7
StringBuffer append(char c):
This is an inbuilt method used to append the string representing character argument at the end of the sequence.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("The character is ");
sb.append('e');
System.out.println("Appending Character argument: " + sb);
}
}
Output:
Appending Character argument: The character is e
StringBuffer append(boolean b):
This is an inbuilt method used to append the string representing the boolean value at the end of the sequence
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("The value is ");
sb.append(true);
System.out.println("Appending Boolean argument: " + sb);
}
}
Output:
Appending Boolean argument: The value is true
StringBuffer append(char[] c):
This is an inbuilt method used to append the string representing the character array at the end of the sequence
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
char[] ch = new char[] {'P','r','o','g','r','a','m'};
sb.append(ch);
System.out.println("Appending character array : " + sb);
}
}
Output:
Appending character array : Java Program
StringBuffer append(char[] c, int offset, int length):
This method is used to append the string representing substring of char array to the given sequence.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
char[] ch = new char[] {'i','s',' ','P','r','o','g','r','a','m'};
sb.append(ch,3,7);
System.out.println("Appending character array : " + sb);
}
}
Output:
Appending character array : Java Program
StringBuffer append(CharSequence c):
This method is used to append the string representing the CharSequence at the end of the given sequence
.
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
CharSequence ch = "program";
sb.append(ch);
System.out.println("Appending CharSequence array : " + sb);
}
}
Output:
Appending CharSequence array : Java program
StringBuffer append(CharSequence c, int start, int end):
This method is used to append the string representing the subsequence of CharSequence at the given sequence
Example:
public class Example
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("Java ");
CharSequence ch = "is a program";
sb.append(ch,5,12);
System.out.println("Appending CharSequence array : " + sb);
}
}
Output:
Appending CharSequence array : Java program
Comments
Post a Comment