[Detailed explanation] Basic usage and differences of String, StringBuffer, and StringBuilder

1. String

1. Commonly used input methods

Scanner in = new ScannerSystem.in);
//输入方法一
String s1 = in.next();
//in.next(): 读到空格就停止扫描(输入)。

//输入方法二
String s2 = in.nextLine();
//in.nextLine():读到回车就停止扫描(输入)。

2.Modification of String type

Once a string is defined in Java, it cannot be changed. We can "modify" the string through the String method or by copying the defined string constructs StringBuilder and StringBuffer.

3.Element access

Characters at a certain position in the string can be accessed through string.charAt(int Index), but they cannot be modified through this method.

4.String method:

String s = in.nextLine();
 
 
s.length();
//返回当前字符串的实际长度;
s.charAt(int Index);
//返回下标为Index的字符;
s.substring(int start);
//返回从start开始一直到结束的子串;
s.substring(int start,int end);
//返回范围是[start,end)的子串;
s.compareTo(String anotherString)
//与anotherString进行比较,s大返回正数,s小返回负数,相等返回0;
s.compareTolgnore(String anotherString)
//与anotherString进行比较,s大返回正数,s小返回负数,相等返回0,但忽略大小写;
s.equals(Object anotherObject);
//相等返回true不相等返回false;
s.equalslgnoreCase(String anotherString);
//相等返回true不相等返回false,忽略大小写;
s.cancat(String str)
//连接s和str等效于“+”
s.indexOf(int n/String str);
//从头开始查找n或者str首次出现的位置(若是str返回开始字符的下标),如果没有返回-1;
s.indexOf(int n/String str,int p);
//和上一个方法类似,不同的是从左开始的某个下标往后查找。
s.lastIndexOf(int ch/String str);
//与前面类似,区别在于该方法从字符串的末尾位置向前查找。
s.lastIndexOf(int ch/String str,int p);
//和上一个方法类似,不同的是从左开始的某个下标往前查找。
s.toLowerCase();
//字符串小写转大写;
s.toUpperCase();
//字符串大写转小写;
s.replace(char a,char b);
//将s中的a都替换成b;
s.replaceFirst(String a,String b);
//将s中的第一个子串a替换成b;
s.replaceAll(String a, String b);
//将s中所有a子串替换成b;
 

二、StringBuffer

1.Construction of StringBuffer

StringBuffer s = new StringBuffer();//无参数构造;
StringBuffer s = new StringBuffer(int I);//指定容量构造;
StringBuffer s = new StringBuffer(String s_);//指定字符串构造;

2.StringBuffer methods

StringBuffer s = new StringBuffer();
 
s.Capacity();
//返回当前字符串缓冲区的容量;
s.length();
//返回当前字符串缓冲区的实际长度;
s.append(String str);
//向字符串缓冲区内添加字符串str,;
s.insert(int p,String s);
//在第p+1的位置上插入s;
s.deleteCharAt(int Index);
//删除指点字符,返回字符缓冲区;
s.delete(int start,int end);
//删除[start,end)的字符,返回字符缓冲区;
s.substring(int start);
//返回从start开始一直到结束的子串;
s.substring(int start,int end);
//返回范围是[start,end)的子串;
s.replace(int start,int end,String str)
//将[start,end)范围内的字符串和str进行替换,返回字符缓冲区;
s.toString();
//将可变长字符串以字符串的形式返回;
s.reverse();
//将字符串s反转;

3. StringBuilder

1.Construction of StringBuilder

StringBuilder s = new StringBuilder();
//无参数构造;
StringBuilder s = new StringBuilder(int I);
//指定容量构造;
StringBuilder s = new StringBuilder(String s_);
//指定字符串构造;

2.StringBuilder methods

StringBuilder s = new StringBuilder();
 
s.Capacity();
//返回当前字符串缓冲区的容量;
s.length();
//返回当前字符串缓冲区的实际长度;
s.append(String str);
//向字符串缓冲区内添加字符串str,;
s.insert(int p,String s);
//在第p+1的位置上插入s;
s.deleteCharAt(int Index);
//删除指点字符,返回字符缓冲区;
s.delete(int start,int end);
//删除[start,end)的字符,返回字符缓冲区;
s.substring(int start);
//返回从start开始一直到结束的子串;
s.substring(int start,int end);
//返回范围是[start,end)的子串;
s.replace(int start,int end,String str)
//将[start,end)范围内的字符串和str进行替换,返回字符缓冲区;
s.reverse();
//将字符串s反转;
s.toString();
//将可变长字符串以字符串的形式返回;

4. Difference

  1. Both StringBuilder and StringBuffer can be converted to String type through toString()

  2. String can be converted from String to StringBuilder through type conversion: StringBuilder(String s).

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

  4. Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified multiple times without creating new unused objects.

  5. 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).

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

  7. StringBuffer is thread-safe, but StringBuilder does not have thread functionality, so its performance will be higher.
    Insert image description here

Guess you like

Origin blog.csdn.net/weixin_43821215/article/details/131143732