Java-String常用类
String类1.字符串常量池定义字符串常量池是 JVM 在堆内存中开辟的一块特殊存储区域用于缓存程序中出现的字符串字面量如abc以及通过intern()方法手动入池的字符串。其核心机制是“先用先存、后续复用”——相同的字符串在池中只保留一份唯一实例后续使用相同内容的字面量时直接返回池中引用从而避免重复创建对象达到节省内存和提升性能的目的。关键点说明位于堆内存存储内容字符串字面量如hello 手动intern()的字符串核心机制哈希表类似 HashMap存储key是字符串内容值是引用复用规则创建string前先查表存在则复用不存在则新建并存入唯一性相同内容的字符串在池中只有一份目的节省内存哈希表哈希表 数组 哈希函数 冲突解决机制用空间换时间实现 O(1) 的极速查找是字符串常量池、HashMap、数据库索引等无数系统的底层基石。和考研的时候做的题目一样。如何将字符串插入哈希表java Key: hello ↓ hash(hello) 1234567 ↓ 1234567 % 10 7 ↓ 存入数组下标 [7] 的位置 如何使用字符串使用的话先去常量池中找是否存在字符串有的话就把引用拿过来进行复用没有的话就是直接赋值虽然表面上看的是赋值但底层和普通类一样不过是jvm创建的对象。直接赋值string s Where there is a will, there is a way.;构造方法赋值string s new string(Where there is a will, there is a way.);2.String的比较1. 当 两边是基本数据类型的时候比较的是值当 两边是引用数据类型比较的是地址比较基本数据类型public static void main(String[] args) { int a 10; int b 20; int c 10; // 对于基本类型变量⽐较两个变量中存储的值是否相同 System.out.println(a b); // false System.out.println(a c); // true }比较引用数据类型// 对于引⽤类型变量 ⽐较两个引⽤变量引⽤的是否为同⼀个对象 public static void main(String[] args) { String s1 new String(hello); String s2 new String(hello); String s3 new String(world); String s4 s1; System.out.println(s1 s2); // false System.out.println(s2 s3); // false System.out.println(s1 s4); // true }这里的s4 s1是将s1在常量池中的地址赋值给s4。2.equals 比较每个字母String s1 new String(hello); String s2 new String(hello); String s3 new String(Hello); System.out.println(s1 s2); // false System.out.println(s1 s3); // false // s1、s2、s3引⽤的是三个不同对象因此⽐较结果全部为false // equals⽐较String对象中的逐个字符 // 虽然s1与s2引⽤的不是同⼀个对象但是两个对象中放置的内容相同因此输出true // s1与s3引⽤的不是同⼀个对象⽽且两个对象中内容也不同因此输出false System.out.println(s1.equals(s2)); // true System.out.println(s1.equals(s3)); // false3.equalsIgnoreCase忽略字母大小写比较输出的是true/falseString s1 Hello; String s2 hello; System.out.println(s1.equalsIgnoreCase(s2)); // true4.compareTo ()当两个字符串长度一样的时候按照ASCII 码表的数值进行比较两个字符串长度不一样的时候输出长度差值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个字符完全相同输出⻓度差值 -3b 的ascll码值比 c 小 1 所以输出 -13.String 的不可变性什么叫作字符串不可变性字符串的不可变性就是说字符串创建之后就存入常量池中无法改变。这个对象的参数就被固定了如果想要改变这个字符串的内容通过改变引用让他指向常量池中的其他字符串。对字符串所有的操作都是创建新的字符串对象而不是进行对原有的字符串修改String s abc; s s def;这个不是字符串s从abc变为abcdef而是在常量池中有创建了一个新的对象abcdef并改变s的引用指向新创建的对象String 的常用方法以下对字符串的所有操作中都是新建新的字符串对象并改变引用指向并非在修改原有的字符串。1.获取长度length()String s hello; System.out.println(s.length()); // 52. 判断是否为空字符串isEmpty()String s ; System.out.println(s.isEmpty()); // true空字符串和null不是一个代表字符串内容是空的null代表这个字符串对象引用为空3. 获取指下标的字符charAt(int index)String s hello; System.out.println(s.charAt(1)); // e4. 拼接字符串concat()String s1 hello; String s2 s1.concat( world); System.out.println(s2);concat()不会改变s1的指向而是创建一个新的String对象然后让s2指向这个新对象类似于号拼接5.字符串包含contains()验证某个字符串中是否包含指定的子串String s hello world; System.out.println(s.contains(world)); // true6. 查找位置indexOf()String s hello; System.out.println(s.indexOf(l)); // 27. 最后一次出现的位置lastIndexOf()String s abca; System.out.println(s.lastIndexOf(a)); // 38. 截取子串substring()String s hello; System.out.println(s.substring(1)); // ello System.out.println(s.substring(1, 4)); // ell字符串中一般是左闭右开区间[ a, b )9. 替换字符replace()String s hello; System.out.println(s.replace(l, x)); // hexxo10. 大小写转换String s Hello; System.out.println(s.toUpperCase()); // HELLO System.out.println(s.toLowerCase()); // hello11. 去除首尾空格trim()中间的空格去除不了String s hello ; System.out.println(s.trim()); // hello12. 字符串拆分split()1.通过单个字符拆分String[] arr s.split( a b);将字符串按照字符a拆分拆分成b组String str hello world hello bit ; String[] result str.split( ,2) ; //按照空格拆分拆分成2组 // hello // world hello bit2.通过多个字符拆分如果⼀个字符串中有多个分隔符可以⽤|作为连字符.|在正则表达式中表⽰或意味着匹配它左边或右边的表达式。|和[]都可以String str namezhangsanage18 ; String[] result str.split( ) ; String[] result str.split([ ]) ; //以上两种都表示按照 或者 拆分13. 转换为字符数组toCharArray()将字符串转换为字符数组String s abc; char[] arr s.toCharArray();15.intern⽅法当调⽤intern()⽅法时如果字符串常量池中已经包含⼀个等于此String对象的字符串则返回常量池中的字符串。会将此String对象添加到常量池中并返回此String对象的引⽤。public static void main(String[] args) { char[] ch new char[]{a, b, c}; String s1 new String(ch); // s1对象并不在常量池中 //s1.intern(); // s1.intern()调⽤之后会将s1对象的引⽤放⼊到常量池中 String s2 abc; // abc 在常量池中存在了s2创建时直接用常量池中abc的引⽤ System.out.println(s1 s2); } // 输出false // 将上述⽅法打开之后就会输出true