*前提知识
String str = “abc”;//"abc"就是String类下的一个具体的对象
这个String类不可以被继承,不能有子类
String底层是一个char类型的数组(字符串底层就是一个char类型的数组)
一、常用方法
1、构造器:底层就是给对象底层的value数组进行赋值操作。
//通过构造器来创建对象:
String s1 = new String();
String s2 = new String("abc");
String s3 = new String(new char[]{'a','b','c'});
2、常用方法
String s4 = "abc";
System.out.println("字符串的长度为:"+s4.length());
String s5 = new SZtring("abc");
System.out.println("字符串是否为空:"+s5.isEmpty());
System.out.println("获取字符串的下标对应的字符为:"+s5.charAt(1));
4、equals:
String s6 = new String("abc");
String s7 = new String("abc");
System.out.println(s6.equals(s7));
equals内部原理:
5、String中对compareTo的重写
String s8 = new String("abc");
String s9 = new String("abc");
System.out.println(s8.compareTo(s9));
compareTo内部原理
6、常用方法
字符串的截取——>substring(3)
字符串的合并/拼接——>concat("ppppp")
字符串中的字符替换——>replace('a', 'u')
转大小写的方法——>toUpperCase()/toLowerCase()
去除首尾的空——>trim()
转换为String类型——>valueOf()//控制台直接输出括号中的内容
//字符串的截取:
String s10 = "abcdefhijk";
System.out.println(s10.substring(3));//defhijk
System.out.println(s10.substring(3, 6));//[3,6) def
//字符串的合并/拼接
System.out.println(s10.concat("ppppp"));//abcdefhijkppppp
//字符串中的字符替换:
String s11 = "abcdefhija";
System.out.println(s11.replace('a', 'u'));//ubcdefhiju
//按照指定的字符串进行分裂为数组的形式:
String s12 = "a-b-c-d-e-f";
String[] strs = s12.split("-");
System.out.println(Arrays.toString(strs));//[a, b, c, d, e, f]
//转大小写的方法:
String s13 = "abc";
System.out.println(s13.toUpperCase());//ABC
System.out.println(s13.toUpperCase().toLowerCase());//abc
//去除首尾的空
String s14 = " a b c ";
System.out.println(s14.trim());//a b c
//toString()
String s15 = "abc";
System.out.println(s15.toString());//abc
//转换为String类型;
System.out.println(String.valueOf(false));//false
PS:图片内容为转载网课老师