目录
一.简单认识String
二.String对象的比较
1.equals
内部实现原理:
2.compareTo
3.compareToIgnoreCase
三.字符串查找
示例:
四.字符串与其他类型转化
1.数值和字符串相互转换
2.大小写相互转化
3.字符串转数组
4.格式化转化
五.字符串替换
全部替换
部分替换
六.字符串拆分
全部拆分
部分拆分
特殊拆分
七.字符串的截取
全部截取
部分截取
一.简单认识String常见的初始化一个字符串有以下几个方式:
public static void main(String[] args) { // 使用常量串构造 String s1 ="hello world"; System.out.println(s1); // 直接newString对象 String s2 = new String("hello world"); System.out.println(s1); // 使用字符数组进行构造 char[] array = {'h','e','l','l','o','w','o','r','l','d'}; String s3 = new String(array); System.out.println(s1); }String是引用类型,内部并不存储字符串本身
二.String对象的比较对于内置类型,==比较的是变量中的值;对于引用类型==比较的是引用中的地址,因此我们在对比俩个字符串的时候是不能直接用等号去判断的,在这里有以下三种方法最为常用
1.equalsbooleanequals(Object anObject) 是按照字符大小的顺序来比较
内部实现原理: public boolean equals(Object anObject){ // 1. 先检测this 和 anObject 是否为同一个对象比较,如果是返回true if (this == anObject) { return true; } // 2. 检测anObject是否为String类型的对象,如果是继续比较,否则返回false if (anObject instanceof String) { // 将anObject向下转型为String类型对象 String anotherString = (String) anObject; int n = value.length; // 3. this和anObject两个字符串的长度是否相同,是继续比较,否则返回false if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; // 4. 按照字典序,从前往后逐个字符进行比较 while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; } public static void main(String[] args) { String s1 = new String("hello"); String s2 = new String("hello"); String s3 = new String("Hello"); // s1、s2、s3引用的是三个不同对象,因此==比较结果全部为false System.out.println(s1 == s2); // false System.out.println(s1 == s3); // false // equals比较:String对象中的逐个字符 // 虽然s1与s2引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true // s1与s3引用的不是同一个对象,而且两个对象中内容也不同,因此输出false System.out.println(s1.equals(s2)); // true System.out.println(s1.equals(s3)); // false } 2.compareToint compareTo(String s) 与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型,compareTo返回的是字符串的差值: 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值,如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
public static void main(String[] args) { String s1 = new String("abc"); String s2 = new String("ac"); String s3 = new String("abc"); String s4 = new String("abcdef"); System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1 System.out.println(s1.compareTo(s3)); // 相同输出 0 System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3 } 3.compareToIgnoreCaseint compareToIgnoreCase(String str) 与compareTo方式相同,但是忽略大小写比较
public static void main(String[] args) { String s1 = new String("abc"); String s2 = new String("ac"); String s3 = new String("ABc"); String s4 = new String("abcdef"); System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1 System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0 System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同,输出长度差值 -3 } 三.字符串查找字符串查找也是字符串中非常常见的操作,String类提供的常用查找的方法:
方法功能charcharAt (int index) 返回 index 位置上字符,如果 index 为负数或者越界,抛出 IndexOutOfBoundsException 异常 int indexOf (int ch) 返回 ch 第一次出现的位置,没有返回 -1 int indexOf (int ch, int fromIndex) 从 fromIndex 位置开始找 ch 第一次出现的位置,没有返回 -1 int indexOf (String str) 返回 str 第一次出现的位置,没有返回 -1 int indexOf (String str, int fromIndex) 从 fromIndex 位置开始找 str 第一次出现的位置,没有返回 -1 int lastIndexOf (int ch) 从后往前找,返回 ch 第一次出现的位置,没有返回 -1 int lastIndexOf (int ch, int fromIndex) 从 fromIndex 位置开始找,从后往前找 ch 第一次出现的位置,没有返 回 -1 int lastIndexOf (String str) 从后往前找,返回 str 第一次出现的位置,没有返回 -1 int lastIndexOf (String str, int fromIndex) 从 fromIndex 位置开始找,从后往前找 str 第一次出现的位置,没有返回-1 示例: public static void main(String[] args) { String s ="aaabbbcccaaabbbccc"; System.out.println(s.charAt(3)); // 'b' System.out.println(s.indexOf('c')); // 6 System.out.println(s.indexOf('c', 10)); // 15 System.out.println(s.indexOf("bbb")); // 3 System.out.println(s.indexOf("bbb", 10)); // 12 System.out.println(s.lastIndexOf('c')); // 17 System.out.println(s.lastIndexOf('c', 10)); // 8 System.out.println(s.lastIndexOf("bbb")); // 12 System.out.println(s.lastIndexOf("bbb", 10)); // 3 } 四.字符串与其他类型转化 1.数值和字符串相互转换可以使用valueOf方法将数值转化为字符串
class Student { String name; int age; public Student(String name, int age) { this.name = name; this.age = age; } } public static void main(String[] args) { // 数字转字符串 String s1 = String.valueOf(1024); String s2 = String.valueOf(12.24); String s3 = String.valueOf(true); String s4 = String.valueOf(new Student("张三", 18)); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); }我们想将整形的双精度浮点型的数值转化为字符串就使用它的包装类 Integer、Double 以及相对应的方法
public static void main(String[] args) { // 字符串转数字 int data1 = Integer.parseInt("1024"); double data2 = Double.parseDouble("10.24"); System.out.println(data1); System.out.println(data2); } 2.大小写相互转化 小写转大写:toUpperCase 大写转小写:toLowerCase public static void main(String[] args) { String s1 ="hello"; String s2 ="HELLO"; // 小写转大写 System.out.println(s1.toUpperCase()); // 大写转小写 System.out.println(s2.toLowerCase()); } 3.字符串转数组可以使用toCharArray方法来将字符串转化为字符数组
public static void main(String[] args) { String s ="hello"; // 字符串转数组 char[] ch = s.toCharArray(); for (int i = 0; i < ch.length; i++) { System.out.print(ch[i]); } System.out.println(); // 数组转字符串 String s2 = new String(ch); System.out.println(s2); } 4.格式化转化使用format方法可以帮助我们将带有格式的字段转化为字符串
public static void main(String[] args) { String s = String.format("%d-%d-%d", 2023, 11, 23); System.out.println(s); } 五.字符串替换Java允许我们使用一段字符串替换掉原有字符串中的部分字段,我们可以有以下俩种方式
全部替换我们可以使用 replaceAll方法,该方法允许我们将整个字符串中选中的部分全部替换
public static void main(String[] args) { String str1 ="helloworld"; System.out.println(str1.replaceAll("l","_")); } 部分替换我们可以使用 replaceFirst方法来将字符串中首个出现的选中的字符进行替换
public static void main(String[] args) { String str2 ="helloworld"; System.out.println(str2.replaceFirst("l","_")); }注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串
六.字符串拆分Java中也允许我们全部拆分字符串或者部分拆分字符串
全部拆分我们可以使用 split方法来做到全部拆分
public static void main(String[] args) { String str ="hello world hello friend"; String[] result = str.split("") ; // 按照空格拆分 for(String s: result) { System.out.println(s); } } 部分拆分我们可以使用 split方法来做到部分拆分,只需要再额外传入一个参数就可以,我们在第二个参数传入多少就会被分成多少分,默认从左往右分
public static void main(String[] args) { String str ="hello world hello friend"; String[] result = str.split("",2) ; for(String s: result) { System.out.println(s); } } 特殊拆分拆分是特别常用的操作,另外有些特殊字符作为分割符可能无法正确切分,需要加上转义,比如我们这里的 “ . ” 在正常情况下是无法识别的,需要加转义符
public static void main(String[] args) { String str ="192.168.1.1"; String[] result = str.split("\\.") ; for(String s: result) { System.out.println(s); } }注意事项:
字符"|","*","+"都得加上转义字符,前面加上 "\\"而如果是"\",那么就得写成"\\\\" 如果一个字符串中有多个分隔符,可以用"|"作为连字符示例:
public static void main(String[] args) { String str ="name=zhangsan&age=18"; String[] result = str.split("&") ; for (int i = 0; i < result.length; i++) { String[] temp = result[i].split("=") ; System.out.println(temp[0]+"="+temp[1]); } } 七.字符串的截取Java允许我们从一个完整的字符串之中截取出部分
全部截取我们可以使用 substring方法从我们指定的索引截取到结尾
public static void main(String[] args) { String str ="helloworld"; System.out.println(str.substring(5)); } 部分截取我们可以使用 substring方法加上一个参数后,只截取一部分内容
public static void main(String[] args) { String str ="helloworld"; System.out.println(str.substring(0, 5)); }其中需要注意:索引从0开始,并且是属于前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标