String
1. String 的构造方法
字符串常量引用通常打印内容而非地址,因为编译器重写了 toString 方法。
public class Test {
public static void main(String[] args) {
// 用常量字符串构造
String s1 = "hello";
System.out.println(s1);
// 用字符数组构造
char[] array = new char[]{'a', 'b', 'c'};
String s2 = new String(array);
System.out.println(s2);
char[] array1 = new char[]{'a', 'b', 'c'};
String s4 = new String(array1, 0, 2); // 从 0 位置后拿 2 个字符
System.out.println(s4);
// 直接 new String 对象构造
String s3 = new String("hello");
System.out.println(s3);
}
}
2. String 对象在内存中的情况
String 对象在堆内存中分配,常量池优化了重复字符串的存储。
3. 空指针异常和空字符串
使用 isEmpty() 判断是否为空字符串,避免空指针异常。
字符串比较
s1 == s2比较地址s1.equals(s2)比较是否相等,返回 true 或者 falses1.compareTo(s2)比较大小s1.compareToIgnoreCase(s2)忽略大小写比较
public class Test {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("Hello");
System.out.println(s1 == s2); // 不等于,s1 和 s2 表示对象的引用都存的是地址
System.out.println(s1.equals(s2));
System.out.println(s1.compareTo(s2)); // s1 大于 s2 返回正数,小于返回负数,等于返回 0
System.out.println(s1.compareToIgnoreCase(s2)); // 忽略大小写比较
}
}
字符串查找
char charAt(int index):返回数组中下标对应的字符
public static void main(String[] args) {
String s1 = new String("hello");
char ch = s1.charAt(1);
System.out.println(ch); // e
}
int indexOf(char ch):返回第一次出现 ch 字符的下标
String s2 = new String("hello");
int index = s2.indexOf('l');
System.out.println(index); // 2
int indexOf(char ch, int k):k 表示下标,从指定位置开始查找
String s2 = new String("hello");
int index = s2.indexOf('l', 3);
System.out.println(index); // 3
int indexOf(String s):可以查找子串在主串中出现的位置,如果没有找到返回 -1
String s3 = "ababcdeabcf";
int index = s3.indexOf("abc");
System.out.println(index); // 2
int index1 = s3.indexOf("abc", 3);
System.out.println(index1); // 7
int lastIndexOf(String s):倒着往前找,返回第一个找到的下标
String s3 = "ababcabcd";
int index = s3.lastIndexOf("abc");
System.out.println(index); // 5
int index1 = s3.lastIndexOf("abc", 4);
System.out.println(index1); // 2 // 从 4 下标位置倒着往前找
转化
- 数字和字符串之间的转化
String s2 = String.valueOf(new Object());
System.out.println(s2);
String s3 = String.valueOf(123);
System.out.println(s3);
String s4 = String.valueOf(123.34);
System.out.println(s4);
String s5 = String.valueOf(true);
System.out.println(s5); // true
- 字符串转数字
int a = Integer.parseInt("190");
System.out.println(a);
// int b = Integer.parseInt("19.9"); // 错误,给的要是整数的字符串
- 大小写转化
小写转大写:toUpperCase()。转变为大写不是在原有字符串的基础上转换,而是转变为大写是一个新的对象,不会改变原有的字符串。
String s = "hello";
String ret = s.toUpperCase();
System.out.println(ret);
大写转小写:
String s = "HEllo";
String ret = s.toLowerCase();
System.out.println(ret); // hello
- 字符串转为数组
String s = "hello";
char[] ret = s.toCharArray();
System.out.println(java.util.Arrays.toString(ret)); // [h,e,l,l,o]
数组转为字符串:
char[] ret = {'a', 'b', 'c'};
String s = new String(ret);
System.out.println(s); // abc
- 格式转换
String s = String.format("%d-%d-%d", 2019, 9, 20);
System.out.println(s); // 2019-9-20
字符串替换
- 替换字符串
- 替换单个字符
- 替换第一个匹配项
- 把所有的匹配项都替换
字符串拆分
s.split()以这里的字符串为标准分割
public static void main(String[] args) {
String s = "hello world k";
String[] array = s.split(" ");
System.out.println(java.util.Arrays.toString(array)); // [hello,world,k]
String s1 = "hello world k";
String[] array1 = s1.split(" ", 2); // 以空格分割最多分成两组
System.out.println(java.util.Arrays.toString(array1)); // [hello, world k]
}
- 特殊的情况,使用转义字符
多次分割时需注意正则表达式的特殊性。
字符串截取(常用)
substring(a,b):[a,b),a 和 b 均为下标
String str = "hello";
String ret = str.substring(0, 3); // hel
- 给一个参数,会把后面的全不截取
String str = "hello";
String ret = str.substring(0); // hello
trim():去掉字符串的左右空格,中间的空格不可去掉
字符串的不可变性
- 字符串中的 value[] 数组是被 private 修饰的,也没有提供 get 方法,在类外是无法拿到的,就无法修改该数组了。
- 而被 final 修饰的,只是表明它是常量了,它的引用只能指向一个对象,不能被改变成指向别的对象。
- 被 final 修饰的不能被继承。
final int[] array = {1, 2, 3};
// array = new int[]{1,2}; // 不能改变 array 的指向了
array[0] = 2; // 可被修改


