javase personal garbage review notes 05 Java StringBuffer and StringBuilder class

Java StringBuffer and StringBuilder classes
When modifying strings, you need to use StringBuffer and StringBuilder classes.

Different from the String class, objects of the StringBuffer and StringBuilder classes can be modified multiple times without generating new unused objects.

The StringBuilder class was proposed in Java 5. The biggest difference between it and StringBuffer is that the methods of StringBuilder are not thread-safe (cannot be accessed synchronously).

Because StringBuilder has a speed advantage over StringBuffer, it is recommended to use StringBuilder class in most cases. However, if the application requires thread safety, the StringBuffer class must be used.

The StringBuffer method
public StringBuffer append(String s)
appends the specified string to this character sequence.

public class append {
    
    
    public static void main(String []args){
    
    
        StringBuffer sb1 = new StringBuffer("gao ze wei zhen shuai.");
        StringBuffer sb2 = new StringBuffer("gao ze wei zhen shuai.");
        StringBuffer sb3 = new StringBuffer("gaozewei+");

        String a = "gao ze wei";
        String b = "zhen shuai.";
        String c = "";
        String d = "gzwgehsrtsh";
        sb1.append(sb2);
        c = a + b;
        sb3.append(d, 3, 9);
        System.out.println(sb1);
        System.out.println(c);
        System.out.println(sb3);
    
    }
}

public StringBuffer reverse()
replaces this character sequence with its reversed form.

public delete(int start, int end)
removes the characters in the substring of this sequence.

public class Test {
    
    

     public static void main(String args[]) {
    
    
          StringBuffer sb = new StringBuffer("Welcom to yiibai.com");
          sb.delete(3, 7); 
          System.out.println(sb); 
       }  
}
Java
执行上面示例代码,得到以下结果:
Welto yiibai.com

public insert(int offset, int i)
inserts the string representation of the int parameter into this sequence.

public class StringBufferDemo {
    
    

  public static void main(String[] args) {
    
    
    StringBuffer buff = new StringBuffer("zyxwvut");
    System.out.println("buffer = " + buff);
    buff.insert(2, 989);
    System.out.print("After insertion = ");
    System.out.println(buff.toString());
  }      
}

/*	让我们来编译和运行上面的程序,这将产生以下结果:
buffer = zyxwvut
After insertion = zy989xwvut

replace(int start, int end, String str)
replaces the characters in the substring of this sequence with the characters in the given String.

 String str3="abcd";
 String str4=str3.replace("ab","ff");
 System.out.println(str4);
 输出结果为:
 ffcd
 StringBuffer str1=new StringBuffer("abcde");
 StringBuffer str2=str1.replace(0,1,"ff");
 System.out.println(str2);
 输出结果为:
 ffbcde

The methods in the following list are similar to those of the String class:
1 int capacity()
returns the current capacity.
2 char charAt(int index)
returns the char value at the specified index in this sequence.
3 void ensureCapacity(int minimumCapacity)
ensures that the capacity is at least equal to the specified minimum value.
4 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
copies the characters from this sequence to the target character array dst.
5 int indexOf(String str)
returns the index of the first occurrence of the specified substring in the string.
6 int indexOf(String str, int fromIndex)
starts at the specified index and returns the index of the first occurrence of the specified substring in the string.
7 int lastIndexOf(String str)
returns the index of the specified substring that appears on the far right in this string.
8 int lastIndexOf(String str, int fromIndex)
returns the last occurrence position of the substring in the String object.
9 int length()
returns the length (number of characters).
10 void setCharAt(int index, char ch)
Set the character at the given index to ch.
11 void setLength(int newLength)
sets the length of the character sequence.
12 CharSequence subSequence(int start, int end)
returns a new character sequence which is a subsequence of this sequence.
13 String substring(int start)
returns a new String that contains the character subsequence currently contained in this character sequence.
14 String substring(int start, int end)
returns a new String, which contains the character subsequence currently contained in this sequence.
15 String toString()
returns the string representation of the data in this sequence.

Guess you like

Origin blog.csdn.net/qq_45864370/article/details/108537582