Summary of common methods of Java StringBuffer class

Keywords: Java

Praise and encourage those you like~

**

StringBuffer class

**

public final class StringBuffer
extends Object
implements Serializable,CharSequence

StringBuffer is also called string buffer. It can become a character sequence and is thread safe. The object entities of the StringBuffer class have a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it will automatically become larger.
Note: the character sequence of a String object cannot be modified.

Construction method of StringBuffer class

StringBuffer()

Construct a character buffer without characters, with an initial capacity of 16 characters.

StringBuffer(int capacity)

Construct a string buffer without characters and the specified initial capacity.  

StringBuffer(String str)

Constructs a string buffer initialized to the specified string content.  

Common methods of StringBuffer class

① Add string
StringBuffer append(T t)
Meaning: append the string representation of parameter t to the end of this sequence. T can be boolean or char
,char[] ,double,float,int,long,Object,String,StringBuffer.
Only character arrays are allowed. Other types of arrays are not allowed.
You can also specify the index values at the beginning and end of the character array and take part of them.

StringBuffer s1=new StringBuffer();
boolean b=true;
s1.append(b);
char[] c={'a','p','p','l','e','s','l','o'};
s1.append(c);
int i=7777;
s1.append(i);
System.out.println(s1); //Output: trueappleslo



char[] c={'a','p','p','l','e','s','l','o'};
StringBuffer s2=new StringBuffer();
s2.append(c,0,5);
System.out.println(s2);  //Output: apple

② Returns the char character of the specified index
char charAt(int index)

StringBuffer s3=new StringBuffer("Java");
System.out.println(s3.charAt(1));  //Output: a

③ Delete
StringBuffer delete(int start ,int end)
Meaning: delete the characters in the substring of this sequence.

StringBuffer s3=new StringBuffer("Java Hello");
System.out.println(s3.delete(0,5)); //Output: Hello

StringBuffer delectCharAt(int index)
Meaning: delete the characters at the specified position in this sequence.

StringBuffer s3=new StringBuffer("Java Hello");
System.out.println(s3.deleteCharAt(0)); //Output: ava Hello

④ Returns the character index value
int indexOf(String str)
Meaning: returns the index in the string where the specified substring first appears.

StringBuffer s3=new StringBuffer("Java Hello");
System.out.println(s3.indexOf("Hello")); //Output; five

int indexOf(String str,int fromIndex)
Meaning: starting from the specified index, returns the index in the string where the specified substring first appears.

StringBuffer s3=new StringBuffer("Java Hello");
System.out.println(s3.indexOf("a",2)); //Output: 3

int lastIndexOf(String str)
Meaning: returns the index in the string of the last occurrence of the specified substring

StringBuffer s3=new StringBuffer("Java Hello");
System.out.println(s3.lastIndexOf("a"));//Output: 3

int lastIndexOf(String str,int fromIndex)
Meaning: looking to the left from the parameter index value, returns the index in the string of the last occurrence of the specified substring

StringBuffer s3=new StringBuffer("Java Hello");
System.out.println(s3.lastIndexOf("a",2));//Output: 1

⑤ Insert
StringBuffer insert(int offset,T t)
Meaning: inserts the parameter t string representation at the specified offset index position.
T can be: boolean, char, char [], double, float, int, long, Object, String, StringBuffer.

StringBuffer s3=new StringBuffer("Java Hello");
int i=555;
char[] c={'a','p','p','l','e','s','l','o'};
StringBuffer stb=new StringBuffer("MySql");
System.out.println(s3.insert(5,i)); //Output: Java 555Hello
System.out.println(s3.insert(5,stb)); //Output: Java MySQL 555hello
System.out.println(s3.insert(0,c,0,5)); //Output: applejava MySQL 555hello

⑥ Replace
StringBuffer replace(int start,int end,String str)
Meaning: replace the contents of the start index to end index range in this sequence with the specified String string.

StringBuffer s3=new StringBuffer("Java Hello");
System.out.println(s3.replace(0,4,"MySql"));  //Output: MySql Hello

void setCharAt(int index,char ch)
Meaning: the character at the specified index is set to ch.

StringBuffer s3=new StringBuffer("Java Hello");
s3.setCharAt(4,'&');
System.out.println(s3);  //Output: Java & hello

⑦ Reverse
StringBuffer reverse();
Meaning: make the sequence replaced by the opposite.

StringBuffer s3=new StringBuffer("Java Hello");
System.out.println(s3.reverse()); //Output: olleH avaJ

⑧ Return substring
String substring(int start)
Meaning: returns a new String, starting from the index start of the sequence
String substring(int start,int end)
Meaning: returns a new String from the index start of the sequence to the end of the index end

StringBuffer s3=new StringBuffer("Java Hello");
String s=s3.substring(5);
String s2=s3.substring(0,4);
System.out.println(s); //Output: Hello
System.out.println(s2); //Output: Java

Praise and encourage those you like~

Posted by nevvermind on Thu, 14 Oct 2021 14:42:49 -0700