目录
1.三者的区别
String、StringBuffer和StringBuilder的区别 - https://blog.csdn.net/csxypr/article/details/92378336
StringBuilder类也代表可变字符串对象。实际上,StringBuilder和StringBuffer基本相似,两个类的构造器和方法也基本相同。不同的是:StringBuffer是线程安全的,而StringBuilder则没有实现线程安全功能,所以性能略高。
2.String
JAVA String字符串的常用方法 - 知乎 https://zhuanlan.zhihu.com/p/64093275
java中常用的String方法 - 阿溪 - 博客园 https://www.cnblogs.com/liujiquan/p/7808501.html
2.1字符串本质
String 类代表字符串。Java 程序中的所有字符串字面值(如“abc”)都作为此类的对象。字符串本质上是一个字符数组,它们的值在创建后不能被更改,所以字符串是常量;可以把字符串看成是字符数组的包装类,内部声明一个 private final char value [ ];因为String 对象是不可变的,所以可以共享。
2.2构造方法
public static void main(String[] args) {
// 在堆区初始化一个空字符串
String str1 = new String();
// 通过一个字节数组构建一个字符串
byte[] bytes = {97,98,99};
// 通过使用平台的默认字符集解码指定的 byte 数组
// System.out.println(Charset.defaultCharset());
String str2 = new String(bytes);
System.out.println(str2);//abc
byte[] byte2 = {-42,-48};
String str3 = null;
try {
// 使用指定的字符集对字节序列进行解码
str3 = new String(byte2,"GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(str3);//中
// 需求:对一个utf-8的字节序列进行解码
byte[] byte3 = {-28,-72,-83,-28,-72,-83};
try {
// sssString str4 = new String(byte3, "UTF-8");
String str4 = new String(byte3,0,3, "UTF-8");
System.out.println("str4:"+str4);//str4:中
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 通过字符数组构建字符串
char[] c1 = {'a','b','c','中','国'};
// String str5 = new String(c1);
String str5 = new String(c1,0,3);
System.out.println(str5);//abc
String str6 = new String("abc");
String str7 = "abc";
System.out.println(str6 == str7);//false
}
2.2字符串的相等比较
String a = "Hello Word";
String b = "hello word";
System.out.println(a == b);//false
System.out.println(a.equals(b));//false
System.out.println(a.equalsIgnoreCase(b));//true
String c = a;
System.out.println(a == c);//true
System.out.println(a.equals(c));//true
System.out.println(a.equalsIgnoreCase(c));//true
//compareTo()和compareToIgnoreCase()按字典顺序比较两个字符串的大小
String a = "Hello Word";
String b = "hello word";
System.out.println(a.compareTo(b));//-32
System.out.println(a.compareToIgnoreCase(b));//0
2.3查找、判断字符串
//获取字符串指定位置的字符
String str="我是123,123。123";
char indexChar = str.charAt(0);
System.out.println(indexChar);//我
//获取字符串指定范围的字符数组
String a = "Hello Word";
char[] b = new char[10];
a.getChars(0, 5, b, 0);
System.out.println(b);//Hello
//查找某个字符在字符串中首次出现的位置
int firstIndex = str.indexOf("1");
System.out.println(firstIndex);//2
//查找某个字符在字符串中最后出现的位置
int lastIndex = str.lastIndexOf("1");
System.out.println(lastIndex);//10
boolean contains = str.contains("10");
System.out.println(contains);//false
//字符串开头和结尾的判断
String a = "Hello Word";
System.out.println(a.startsWith("ee"));//false
System.out.println(a.startsWith("He"));//true
System.out.println(a.endsWith("rd"));//true
System.out.println(a.endsWith("r"));//false
String str="我是123,123。123";
boolean startWith=str.startsWith("我");
boolean endWith=str.endsWith("3");
//是否为空
String a = "";
System.out.println(a.isEmpty());//true
String b = " ";
System.out.println(b.isEmpty());//false
2.4大小写转换
String str="我是Aa";
//将字母全部转换成大写
String lowerCase = str.toLowerCase();
System.out.println(lowerCase);//我是aa
//将字母全部转换成小写
String upperCase = str.toUpperCase();
System.out.println(upperCase);//我是AA
2.5字符串长度、去空格、分割
String str=" 我是123,123。123 ";
//获取字符串长度
int strLenth = str.length();//15
System.out.println(strLenth);
//去除字符串中的首尾空格
String newStr = str.trim();//我是123,123。123
System.out.println(newStr);
//将字符串分割,多个分隔符可用|隔开,例如下面这个是按照“。”和“,”分割的
String[] splite = str.trim().split("。|,");//
for (int i = 0; i < splite.length; i++)
System.out.println(splite[i]);
//我是123
//123
//123
2.6字符串截取、替换、连接
//截取字符串:
String str="我是123,123。123";
String subStr = str.substring(0,2);//我是
//截取字符串:输入为字符数组
char[] Str1 = {'h', 'e', 'l', 'l', 'o', ' ', 's', 'u', 'n', 'n', 'y', '!'};
String Str2 = "";
Str2 = Str2.copyValueOf( Str1 );
System.out.println("返回结果:" + Str2);//返回结果:hello sunny!
Str2 = Str2.copyValueOf( Str1, 2, 6 );
System.out.println("返回结果:" + Str2);//返回结果:llo su
//字符串替换
String str="我是123,123。123";
String reStr = str.replace("1", "4");//我是423,423。423
String firReStr = str.replaceFirst("1", "4");//我是423,123。123
//正则表达式替换
String Str = new String("www.google.com");
System.out.println(Str.replaceAll("(.*)google(.*)", "runoob" ));//runoob
System.out.println(Str.replaceAll("(.*)taobao(.*)", "runoob" ));//www.google.com
//连接字符串:concact
String a = "Hello Word";
String b = "你好";
System.out.println(b.concat(a));//你好Hello Word
System.out.println(b+a);//你好Hello Word
//连接字符串:join
List names=new ArrayList<String>();
names.add("1");
names.add("2");
names.add("3");
System.out.println(String.join("-", names));//1-2-3
String[] arrStr=new String[]{"a","b","c"};
System.out.println(String.join("-", arrStr));//a-b-c
2.7 字符串转变为Byte数组、字符数组
String a = "Hello Word";
//getBytes()将字符串变成一个byte数组
byte[] b = a.getBytes();
System.out.println(b);//[B@1cd072a9
System.out.println(new String(b));//Hello Word
//toCharArray()将字符串变成一个字符数组
char[] c = a.toCharArray();
System.out.println(c);//Hello Word
2.8字符串与基本类型的转换
//String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 ,
//也就是 String.valueOf() 这个参数多载的方法
int i = 10;
String str = String.valueOf(i);
System.out.println(str);//10
//要将 String 转换成基本数据型态转 ,大多需要使用基本数据型态的包装类别
Integer i = 10;
String str = String.valueOf(i);
System.out.println(str instanceof String);//true
Short j = Short.parseShort(str);
System.out.println(j instanceof Short);//true
3.StringBuffer
public class 字符串 {
public static void main(String args[]){
StringBuffer strBuffer1 = new StringBuffer("Hello");
StringBuffer strBuffer2 = new StringBuffer("*");
//末尾添加
strBuffer1.append(strBuffer2);
strBuffer1.append("wo"+"rld");
strBuffer1.append("!");
System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Hello*world!
// 将指定的字符串插入字符串序列
strBuffer1.insert(0, "Ni Hao, ");
System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Ni Hao, Hello*world!
// 删除指定位置字符串
strBuffer1.delete(strBuffer1.indexOf("N"),strBuffer1.indexOf("*"));
System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = *world!
strBuffer1.deleteCharAt(strBuffer1.indexOf("*"));
System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = world!
//替换指定范围字符串
strBuffer1.insert(0, "*");
strBuffer1.replace(0, strBuffer1.indexOf("*"), "Ni Hao,");
System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Ni Hao,*world!
// 字符串反转
strBuffer1.reverse();
System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = !dlrow ,oaH iN
strBuffer1.reverse();
System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = Ni Hao,*world!
// 修改指定位置的字符
strBuffer1.setCharAt(0, 'n');
System.out.println("strBuffer1 = "+strBuffer1);//strBuffer1 = ni Hao,*world!
//获取StringBuffer对象的容量,和字符串的长度
System.out.println("strBuffer1.capacity() = "+strBuffer1.capacity());//strBuffer1.capacity() = 21
System.out.println("strBuffer1.length() = "+strBuffer1.length());//strBuffer1.length() = 14
//查找指定字符位置
System.out.println(strBuffer1.indexOf("o"));//5
System.out.println(strBuffer1.indexOf("o",6));//9
System.out.println(strBuffer1.lastIndexOf("o"));//9
System.out.println(strBuffer1.lastIndexOf("o",6));//5
// 比较StringBuffer字符串是否相等
StringBuffer strBuffer3 = new StringBuffer("ni Hao,*world!");
boolean flag = strBuffer1.toString().equals(strBuffer3.toString());
System.out.println("strBuffer1 == strBuffer3 : "+flag);//strBuffer1 == strBuffer3 : true
// 遍历StringBuffer,替换’o‘ -> 'O'
for (int i = 0 ; i < strBuffer3.length() ; i++) {
if (strBuffer3.charAt(i) == 'o') {
strBuffer3.replace(i, i+1, "O");
}
}
System.out.println("strBuffer3 = "+strBuffer3);//strBuffer3 = ni HaO,*wOrld!
//获得子串
String string1 = strBuffer1.substring(strBuffer1.indexOf("n"),strBuffer1.indexOf(","));
System.out.println("string1 = "+string1);//strBuffer4 = ni Hao
}
}
4.StringBuilder
StringBuffer是线程安全的,StringBuilder不是线程安全的,方法基本一样