1.常用方法
1.1字符串构造方法
常用的有三种方法用来构造字符串
1.直接使用常量串进行构造
2.使用newString类对象
3.使用字符串数组进行构造
public class demo1 {
public static void main(String[] args) {
//方法一:
String s1="abc";
System.out.println(s1);
//方法二:
String s2=new String("abcd");
System.out.println(s2);
//方法三:
char [] array={'a','b','c','d','e'};
String s3=new String(array);
System.out.println(s3);
}
}
注意:字符串是引用类型,所以内部并不保存实际字符串内容,而是保存地址。
1.2String对象的比较
字符串的比较是常用的方法之一,以下提供四种比较方法:
1.利用==比较引用对象(即比较地址)
对于内置变量(即简单变量),直接比较内容,而对于引用对象则是比较对象地址
public static void main(String[] args) {
int a=10;
int b=20;
String s1="abc";
String s2="abc";
System.out.println(a==b);
System.out.println(s1==s2);
//简单变量直接比较内容
String s3=new String("abc");
String s4=new String("abc");
String s5=new String("abcd");
String s6=s1;
System.out.println(s3==s4);
System.out.println(s3==s5);
System.out.println(s6==s1);
//引用变量比较地址
}
}
输出结果:
2.boolean equals(Object anObject) 方法
equals方法可以比较引用对象的内容,String类重写父类object的equals方法,因为父类object中的equals方法是按照==进行比较的,重写后按照如下的规则进行比较(原理)
// public boolean equals(Object obj) {
1.先检测obj和this是否是同一个对象,如果不是再继续比较
// if (obj == this) {
// return true;
// }
2.检测obj是不是String类的对象,如果是则继续比较
// if (obj instanceof String) {
//将obj强转成String类的对象
// String anobj = (String) obj;
// int n = this.length;
// if (n == anobj.value.length) {
// char v1[] = value;
// char v2[] = anobj.value;
// int i = 0;
// while (n-- != 0) {
// if (v1[i] != v2[i])
// return false;
// i++;
// }
// return true;
//
// }
// }
//return false;
// }
3.int compareTo(String s) 方法
按照字典序进行比较,compareTo返回的是int类型。
具体比较方式:
1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
public class demo2 {
public static void main(String[] args) {
String s1=new String("abc");
String s2=new String("abcd");
String s3=new String("abc");
String s4=new String("ad");
System.out.println(s1.compareTo(s2));//前三项相等,输出长度差-1
System.out.println(s1.compareTo(s3));//完全相等,输出0
System.out.println(s1.compareTo(s4));//第二项不同,输出不同项的字符差-2
}
}
输出结果:
4.int compareToIgnoreCase(String str) 方法
与compareTo方式相同,但是忽略大小写比较
public class demo2 {
public static void main(String[] args) {
String s1=new String("ABC");
String s2=new String("abCd");
String s3=new String("abc");
String s4=new String("aD");
System.out.println(s1.compareToIgnoreCase(s2));
System.out.println(s1.compareToIgnoreCase(s3));
System.out.println(s1.compareToIgnoreCase(s4));
}
}
输出结果:
1.3字符串的查找
1.char charAt(int index):返回index位置上的字符
String s1=new String("aaabbbcccddd");
System.out.println(s1.charAt(8));
//输出c
2.int indexOf(int ch):返回ch第一次出现的位置,没有返回-1
String s1=new String("aaabbbcccddd");
System.out.println(s1.indexOf("c"));
//输出:6
3.int indexOf(int ch, int fromIndex):从fromlndex位置开始找ch字符第一次出现的位置,没有返回-1
String s1=new String("aaabbbcccddd");
System.out.println(s1.indexOf("c",8));
//输出:8
4.int indexOf(String str):返回str第一次出现的位置,没有返回-1
String s1=new String("aaabbbcccddd");
System.out.println(s1.indexOf("d"));
//输出:9
5.int indexOf(String str, int fromIndex):从fromlndex位置开始找str字符第一次出现的位置,没有返回-1
String s1=new String("aaabbbcccddd");
System.out.println(s1.indexOf("d",10));
//输出:10
6.int lastIndexOf(int ch):从后往前找,返回ch第一次出现的位置,没有返回-1
String s1=new String("aaabbbcccddd");
System.out.println(s1.lastIndexOf("c"));
//输出:8(返回值是正序)
7.int lastIndexOf(int ch, int fromIndex):从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返 回-1
String s1=new String("aaabbbcccddd");
System.out.println(s1.lastIndexOf("c",9));
//输出:8
8.int lastIndexOf(String str):从后往前找,返回str第一次出现的位置,没有返回-1
String s1=new String("aaabbbcccddd");
System.out.println(s1.lastIndexOf("bb"));
//输出:4
9.int lastIndexOf(String str, int fromIndex):从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1、
String s1=new String("aaabbbcccddd");
System.out.println(s1.lastIndexOf("bb",3));
//输出:3
1.4字符串的转化
1.4.1数值转化
public static void main(String[] args) {
// 数字转字符串
String s1 = String.valueOf(123);
String s2 = String.valueOf(3.14);
String s3 = String.valueOf(true);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
输出结果:
1.4.2大小写转化
toUppercase:转大写 toLowerCase:转小写
public static void main(String[] args) {
// 数字转字符串
String s1 = "abc";
String s2 = "ABC";
System.out.println(s1.toUpperCase());//输出ABC
System.out.println(s2.toLowerCase());//输出abc
}
1.4.3字符串转数组
public static void main(String[] args) {
//字符串转数组
String s1="abc";
char [] ch=s1.toCharArray();
for (int i = 0; i <ch.length; i++) {
System.out.print(ch[i]);//输出abc
}
System.out.println();
//数组转化成字符串
String s2=new String(ch);
System.out.println(s2);//输出abc
}
1.4.4格式化
public static void main(String[] args) {
String s = String.format("%d-%d-%d", 2022, 9,18);
System.out.println(s);
}
1.5字符串替换
replaceAll()替换所有项
replace()替换第一个项
public static void main(String[] args) {
String s = "my love can fight everything my love";
System.out.println(s.replaceAll("m","a"));
System.out.println(s.replace("y","b"));
}
注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串
1.6字符串拆分
String[] split(String regex) :将字符串全部拆分
String[] split(String regex, int limit): 将字符串以指定的格式,拆分为limit组
public static void main(String[] args) {
String s = "my love can fight everything my love";
String []arr=s.split(" ");
for (String a:arr) {
System.out.println(a);
}
}
输出结果:
注意事项:
1. 字符"|","*","+"都得加上转义字符,前面加上 "\\" .
2. 而如果是 "\" ,那么就得写成 "\\\\" .
3. 如果一个字符串中有多个分隔符,可以用"|"作为连字符.
1.7字符串截取
String substring(int beginIndex) :从指定索引截取到结尾
String substring(int beginIndex, int endIndex): 截取部分内容
public static void main(String[] args) {
String s = "my love can fight everything my love";
System.out.println(s.substring(3));
System.out.println(s.substring(0,5));
}
输出结果:
注意事项:
1. 索引从0开始
2. 注意前闭后开区间的写法, substring(0, 3) 表示包含 0 号下标的字符, 不包含 3 号下标
1.8字符串的不可变性
1.String类是一种不可变的对象,其中字符串的内容同样是不可变的。
2.涉及到修改字符串内容都是新创建一个对象,新建一个字符串进行保存,原字符串不变。
1.9StringBuilder和StringBuffer
为了方便字符串的修改,Java中又提供StringBuilder和StringBuffer类。
具体的使用方法建议查看方法文档。