大佬的理解->《深入理解Java中的String》
1、String 1.1 String 实例化String str1 = "xxx";
String string1 = "hello KH96";
System.out.println(string1); //hello KH96
String str1 = new String("xxx");
String string2 = "hello KH96";
System.out.println(string2); //hello KH96
String底层实现 private final char value[];
String底层是由私有final的数组实现的,对外没有提供修改的方法,字符串多次赋值,不是修改字符串的内容,而是改变字符串的引用地址;
源码
length()
字符串的长度:length()方法,返回的是字符串的长度,即字符串的长度(不是字节数),区别去数组的length
String string1 = "hello KH96";
System.out.println(string1+"的长度:"+string1.length()); //hello KH96的长度:10
equals()
重写了Object类的equals方法,比较的是字符串的内容,不是对象
String string2 = "KH96";
String string3 = "KH97";
System.out.println(string2.equals(string3)); //false
equalsIgnoreCase(String str)
忽略大小比较
String string7 = "kh96";
String string8 = "KH96";
System.out.println("不忽略大小写比较:"+string7.equals(string8)); //false
System.out.println("忽略大小写比:"+string7.equalsIgnoreCase(string8)); //true
toUpperCase() 转大写 toLowerCase() 转小写
String string9 = "abCD";
System.out.println(string9.toUpperCase()); //ABCD
System.out.println(string9.toLowerCase());//abcd
concat(String str) 返回拼接后的字符串
"+"号也可以进行字符串拼接
concat(String str)
拼接字符串都创建了新的对象,在循环中尽量不要拼接字符串,会造成栈溢出;
String strig10 = "abc";
System.out.println(strig10.concat("bcd").concat("def"));//abcbcddef
1.3 String 字符查找/提取相关方法
indexOf(String str)
返回str首次出现的下标,没有查到就返回-1
String string11 = "I am a good student in kh96";
System.out.println("good首次出现的位置:"+string11.indexOf("good")); //7
还可以通过ascii码值查询
String string11 = "I am a good student in kh96";
char char1 = 97;
System.out.println(char1); //a
System.out.println("参数支持int assic码值:"+string11.indexOf(97)); //2
lastIndexOf(String str)
返回str最后一次出现的下标,没有就返回-1
String string11 = "I am a good student in kh96";
System.out.println(string11);
System.out.println("t最后一次出现的下标:"+string11.lastIndexOf("t")); //18
substring(int index1)
截取下标index1,及以后的所有字符
index的范围[0,string.length()]
String string12 = "abcdefghijklmn";
System.out.println(string12.substring(5)); //fghijklmn
substring(int index1,int index2)
截取下标index1到index2之间的字符串,包括index1,不包括index2
index的范围[0,string.length()]
String string12 = "abcdefghijklmn";
System.out.println(string12.substring(5,8)); //fgh
小应用
String string14 = "KH90,KH91,KH92,KH93,KH94,KH95,";
System.out.println(string14.substring(0,string14.lastIndexOf(",")));//KH90,KH91,KH92,KH93,KH94,KH95
trim()
去除字符串的首尾空格
String string13 = " KH 96 ";
System.out.println("原始长度"+string13.length()); //10
System.out.println("取出空格后长度"+string13.trim().length()); //6 "KH 96"
startsWith(String str) endsWith(String str)
startsWith(String str) 是否以str开头
endsWith(String str) 是否以str结尾
String string15 = "KH96.mp3";
System.out.println("是否是KH开头?"+ string15.startsWith("KH")); //true
System.out.println("是否是.mp3结尾?"+ string15.endsWith(".mp3")); //true
contains(String str)
判断字符串是否包含str
String string16 = "aaa bbb cc ddd";
System.out.println("是否包含bbb:"+ string16.contains("bbb")); //true
System.out.println("是否包含eee:"+ string16.contains("eee")); //false
split(String str)
根据指定分割字符,将字符串拆分成字符串数组返回
String string17_1 = "13501020304;15801020304;18901020304";
String[] phoneNumbers1 = string17_1.split(";"); //一种字符分割
System.out.println("手机号数组内容:"+ Arrays.toString(phoneNumbers2));
String string17_2 = "13501020304;15801020304!18901020304";
String[] phoneNumbers2 = string17_2.split(";|!"); //多种字符分割 用 | 隔开
System.out.println("手机号数组内容:"+ Arrays.toString(phoneNumbers2));
//[13501020304, 15801020304, 18901020304]
toCharArray()
将字符串转为字符数组
char[] chars1 = string18.toCharArray();
System.out.println(Arrays.toString(chars1)); //[K, H, 9, 8, 正, 在, 学, 习, 实, 用, 类]
replace(String str1,String str2)
用 str2 替换 str1
//获取一个16位的随机字符串
String string19 = UUID.randomUUID().toString();
System.out.println(string19); //65c0844a-c437-4a65-89ca-84d4166325ff
//转换字符串,将-转换为*
System.out.println(string19.replace("-","*"));//65c0844a*c437*4a65*89ca*84d4166325ff
//去除字符串,将所有的-去除
System.out.println(string19.replace("-",""));//65c0844ac4374a6589ca84d4166325ff
//去16位随机数
System.out.println(string19.replace("-","").substring(0,16));//65c0844ac4374a65
getBytes() getBytes("UTF-8")
字符串转换为字节数组
String string20 = "abcd";
//getBytes() 没有指定编码
byte[] bytes = string20.getBytes();
try {
//getBytes("UTF-8") 指定编码
byte[] bytes2 = string20.getBytes("UTF-8");
System.out.println(Arrays.toString(bytes2)); //[97, 98, 99, 100]
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(Arrays.toString(bytes)); //Arrays.toString(bytes)
new String(byte[] bytes)
将字节数组转换为字符串
byte[] bytes3 ={100,101,102}; //ascii码值
System.out.println(new String(bytes3)); //def
//配合上面getBytes进行转码
try {
System.out.println(new String(bytes3,"utf-8")); //可以指定编码 def
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
char[] chars3 = {'K','H','9','6'};
System.out.println(new String(chars3));//KH96
2、StringBuffer
可变字符串类:StringBuffer
不同于String类:可以实现动态拼接字符串,而不会创建新的对象;
即:是一个可变字符串的对象,改变的是字符串对象中的内容;
不可以直接赋值,必须通过new创建对象;
new StringBuffer()
默认初始容量 16
StringBuffer sbf1 = new StringBuffer();
System.out.println("默认初始容量:"+sbf1.capacity());//16
底层实现
//StringBuffer()
public StringBuffer() {
super(16); //初始容量16
}
//AbstractStringBuilder(int capacity)
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
new StringBuffer(int capacity)
指定初始容量
StringBuffer sbf2 = new StringBuffer(32);
System.out.println("只定始容量:"+sbf2.capacity()); //32
底层实现
public StringBuffer(int capacity) {
super(capacity); //指定初始容量
}
StringBuffer(String str)
指定初始字符串,容量为字符串长度+16
StringBuffer sbf3 = new StringBuffer("Kh96");
System.out.println("指定初始字符串初始容量:"+sbf3.capacity()); //20
底层实现
public StringBuffer(String str) {
super(str.length() + 16); //容量为字符串长度+16
append(str);
}
2.2 StringBuffer常用方法
append(String str)
拼接字符串
StringBuffer sbf4 = new StringBuffer("userId=");
sbf4.append("U0001")
.append(",userName=")
.append("张三,age=")
.append("18"); //userId=U0001,userName=张三,age=18
扩容机制
底层扩容,当拼接一个新的字符串,字符串数组长度不够,会进行动态扩容,
每次扩容都是前一个数组长度的2倍+2
最大扩容长度不能超过Integer的最大值 - 8;
void expandCapacity(int minimumCapacity) {
int newCapacity = value.length * 2 + 2; //每次扩容都是前一个数组长度的2倍+2
if (newCapacity - minimumCapacity < 0)
newCapacity = minimumCapacity;
if (newCapacity < 0) {
if (minimumCapacity < 0) // overflow
throw new OutOfMemoryError();
newCapacity = Integer.MAX_VALUE;
}
value = Arrays.copyOf(value, newCapacity);
}
toString()
获取动态字符串内容
StringBuffer sbf4 = new StringBuffer("userId=");
sbf4.append("U0001")
.append(",userName=")
.append("张三,age=")
.append("18");
String userInfo = sbf4.toString();
System.out.println(userInfo); //userId=U0001,userName=张三,age=18
3、StringBuilder
用法和StringBuffer没有区别,唯一的区别就是StringBuffer是线程安全的,StringBuilder是非线程安全的;
【转自:香港服务器 https://www.68idc.cn提供,感谢支持】