String:字符串 - 字符序列(字符数组)
不可变,存储在堆中的字符串常量池
常用API:
int length()
char charAt(int)
常用API
concat:
将指定字符串连接到此字符串的结尾//字符串拼接
String s1 = "hello"
String s2 = s1.concat(",.");
contains:
当且仅当此字符串包含指定的 char 值序列时,返回 true// s1是否包含指定
System.out.println("hello".contains("lo"));//true
endswith:
测试此字符串是否以指定的后缀结束
System.out.println("hello".endsWith("lo"));//true
indexOf * 4:
- public int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引
System.out.println("hello".indexOf('l'));//2
- public int indexOf(char ch,int fromIndex) 返回在此字符串中第一次出现指定字符
System.out.println("hello".indexOf('l',3));//3
- public int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引
System.out.println("hello".indexOf("ll"));//2
- public int indexOf(String str,int fromIndex) 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
System.out.println("hello".indexOf("ll",2));//2
isEmpty:
当且仅当 length() 为 0 时返回 true
System.out.println("".isEmpty());//true
//String不能是null
lastIndexOf * 4:
用法同 indexOf()
- public int lastIndexOf(int ch) 返回指定字符在此字符串中最后一次出现处的索引
- public int lastIndexOf(int ch,int fromIndex) 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索
- public int lastIndexOf(String str) 返回指定子字符串在此字符串中最右边出现处的索引(返回最后一个这种子字符串的第一个字符)
- public int lastIndexOf(String str,int fromIndex) 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
replace:
使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串
public String replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的
public String replace(CharSequence target,CharSequence replacement) 使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串
(String 实现CharSequence接口,看到CharSequence就用String字符串)
startsWith * 2:
public boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始
public boolean startsWith(String prefix,int toffset) 测试此字符串从指定索引开始的子字符串是否以指定前缀开始
substring * 2:
public String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾
public String substring(int beginIndex,int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符
trim:
返回字符串的副本,忽略前导空白和尾部空白(用于截去字符串开头和末尾的空格字符 \t \n)
字符串要点
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);//false
//常量池:常量,直接复制的字符串,指向常量池
String s3 = "hello";
String s4 = "hello";
System.out.println(s3 == s4);//true
String s6 = "he";
String s7 = "hello";
//常量+常量=》常量一致
String s8 = "he"+"llo";
//变量+常量=》常量不一致
String s9 = s6+"llo";
System.out.println(s7 == s8);
System.out.println(s7 == s9);
StringBuilder、StringBuffer
StringBuilder/StringBuffer
可变长字符串:改变字符串,不会产生新的对象
StringBuffer:所有方法synchronized(锁)
安全,效率低
StringBuilder:不安全,效率高
代码:
//""
StringBuilder sb = new StringBuilder();
//"hello"
sb = new StringBuilder("hello");
//append(obj) - 拼接
sb.append(123);
sb.append(3.14);
sb.append(true);
System.out.println(sb); // hello1233.14true
//insert(index, obj) - 插入
sb.insert(0, false);
System.out.println(sb); // falsehello1233.14true
//delete(start, end) - 删除子串
sb.delete(3, 8);
System.out.println(sb); // fallo1233.14true
//replace(start, end, str) - 替换
sb.replace(0, 8, "haha");
System.out.println(sb); // haha3.14true
//reverse() - 反转
sb.reverse();
System.out.println(sb); // eurt41.3ahah