Join
| 如果需要把多个字符串放在一起,用一个定界符分隔,可以使用静态join方法 |
| String str = String.join("/", "S", "M", "X", "XL"); |
| |
equals
| equals方法检测两个字符串是否相等,返回结果boolean类型 |
| 不区分大小写检测两个字符串是否相等,使用equalsIgnoreCase方法 |
注意:
| 一定不要使用==运算符检测两个字符串是否相等,这个运算符只能确定两个字符串是否放置在同一个位置上。 |
| 如果字符串放置在同一个位置上,它们必然相等,但是完全有可能将内容相同的多个字符串的拷贝放置在不同的位置上。 |
空串与Null串
| 空串 "" 是长度为0的字符串,空串是一个Java对象,有自己的串长度(0)和内容(空)。 |
| |
| String变量还可以存放一个特殊的值,名为null,这表示目前没有任何对象与该变量关联。 |
检查一个字符串既不是null也不为空串,使用以下条件:
| if(str != null && str.length() != 0) {} |
| |
| public class Null { |
| public static void main(String[] args) { |
| String str = null; |
| |
| if (str.length() != 0 && str != null) { |
| System.out.println("str为null先调用方法,报错!"); |
| } |
| } |
| } |
| |
| |
| |
| |
charAt
| public class CharAt { |
| public static void main(String[] args) { |
| String str = "abc"; |
| |
| char c = str.charAt(2); |
| System.out.println(c); |
| } |
| } |
| |
codePointAt
| int codePointAt(int index) 5.0 |
| |
| |
| |
| public class CodePointAt { |
| public static void main(String[] args) { |
| String str = "codePointAt"; |
| |
| int point = str.codePointAt(3); |
| System.out.println(point); |
| } |
| } |
| |
offsetByCodePoints
| int offsetByCodePoints(int startIndex, int cpCount) 5.0 |
| |
| |
| |
| public class OffsetByCodePoints { |
| public static void main(String[] args) { |
| String str = "abcdefg"; |
| |
| |
| int points = str.offsetByCodePoints(3, 2); |
| System.out.println(points); |
| } |
| } |
| |
compareTo
| int compareTo(String other) |
| |
| |
| |
| |
| |
| public class CompareTo { |
| public static void main(String[] args) { |
| String str1 = "abc"; |
| String str2 = "abcd"; |
| String str3 = "abc"; |
| String str4 = "abcdef"; |
| String str5 = "abz"; |
| |
| int i = str1.compareTo(str2); |
| int i1 = str1.compareTo(str3); |
| int i2 = str4.compareTo(str1); |
| int i3 = str1.compareTo(str5); |
| System.out.println(i); |
| System.out.println(i1); |
| System.out.println(i2); |
| System.out.println(i3); |
| } |
| } |
compareTo底层源码
| public int compareTo(@NotNull String anotherString) { |
| int len1 = value.length; |
| int len2 = anotherString.value.length; |
| int lim = Math.min(len1, len2); |
| char v1[] = value; |
| char v2[] = anotherString.value; |
| |
| int k = 0; |
| while (k < lim) { |
| char c1 = v1[k]; |
| char c2 = v2[k]; |
| if (c1 != c2) { |
| return c1 - c2; |
| } |
| k++; |
| } |
| return len1 - len2; |
| } |
常用方法



String类与其他结构之间的转换
字节数组与字符串

| @Test |
| public void test3() throws UnsupportedEncodingException { |
| String str1 = "abc123中国"; |
| byte[] bytes = str1.getBytes(); |
| System.out.println(Arrays.toString(bytes)); |
| |
| byte[] gbks = str1.getBytes("gbk"); |
| System.out.println(Arrays.toString(gbks)); |
| |
| System.out.println("******************"); |
| |
| String str2 = new String(bytes); |
| System.out.println(str2); |
| |
| String str3 = new String(gbks); |
| System.out.println(str3); |
| |
| String str4 = new String(gbks, "gbk"); |
| System.out.println(str4); |
| } |
字符数组与字符串

| @Test |
| public void test2(){ |
| String str1 = "abc123"; |
| |
| char[] charArray = str1.toCharArray(); |
| for (int i = 0; i < charArray.length; i++) { |
| System.out.println(charArray[i]); |
| } |
| |
| char[] arr = new char[]{'h','e','l','l','o'}; |
| String str2 = new String(arr); |
| System.out.println(str2); |
| } |
基本数据类型、包装类与字符串

| @Test |
| public void test1(){ |
| String str1 = "123"; |
| |
| int num = Integer.parseInt(str1); |
| |
| String str2 = String.valueOf(num); |
| String str3 = num + ""; |
| |
| System.out.println(str1 == str3); |
| } |
StringBuffer、StringBuilder、String





对比String、StringBuffer、StringBuilder三者的效率:
从高到低排列:StringBuilder > StringBuffer > String
StringBuffer的常用方法:(与StringBuilder差不多,差在一个同步,一个不同步)
| StringBuffer append(xxx):提供了很多的append()方法,用于进行字符串拼接 |
| StringBuffer delete(int start,int end):删除指定位置的内容 |
| StringBuffer replace(int start, int end, String str):把[start,end)位置替换为str |
| StringBuffer insert(int offset, xxx):在指定位置插入xxx |
| StringBuffer reverse() :把当前字符序列逆转 |
| public int indexOf(String str) |
| public String substring(int start,int end):返回一个从start开始到end索引结束的左闭右开区间的子字符串 |
| public int length() |
| public char charAt(int n ) |
| public void setCharAt(int n ,char ch) |
String、StringBuffer、StringBuilder三者的异同?
- String:不可变的字符序列;底层使用char[]存储 (使用final关键字修饰)
- StringBuffer:可变的字符序列;线程安全的,效率低;底层使用char[ ]存储
- StringBuilder:可变的字符序列;jdk5.0新增的,线程不安全的,效率高;底层使用char[]存储
源码分析:
| String str = new String(); |
| String str1 = new String("abc"); |
| |
| StringBuffer sb1 = new StringBuffer(); |
| System.out.println(sb1.length()); |
| sb1.append('a'); |
| sb1.append('b'); |
| |
| StringBuffer sb2 = new StringBuffer("abc"); |
| |
| |
| |
| |
| |
开发中建议大家使用:StringBuffer(int capacity) 或 StringBuilder(int capacity)
当事先知道需要大量使用append()方法添加,知道容量大小,就使用可以构造指定容量的字符串缓冲区的构造器,避免扩容(容量扩充、数组复制),提高效率。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步