1.API
API概述-帮助文档的使用
什么是API
API (Application Programming Interface) :应用程序编程接口
java中的API指的就是 JDK 中提供的各种功能的 Java类,这些类将底层的实现封装了起来.
Java API是一本程序员的 字典 ,是JDK中提供给我们使用的类的说明文档。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| API的使用步骤: 1.查看类 java.util.Scanner :该类需要import导入后使用。 2.查看构造方法 public Scanner(InputStream source) : 构造一个新的 Scanner ,它生成的值是从指定的输入流扫描的。 3.查看成员方法 public String next() : 获取键盘录入的字符串(不能包含空白字符),遇到空白字符/回车换行就认为录入结束 public String nextLine() : 获取键盘录入的一行字符串(可以包含空白字符),遇到回车换行就认为录入结束
public class Demo01Scanner { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串: "); String str = sc.nextLine(); System.out.println("您输入的字符串: "+str);
System.out.println("请再输入一个字符串: "); String str2 = sc.next(); System.out.println("您输入的第二个字符串: "+str2); } }
|
2. String类
2.1 String概述
1 2 3 4 5 6 7 8 9 10 11 12 13
| java.lang.String类: 用来描述的字符串的
说白了: String 类代表字符串。 Java 程序中的所有字符串字面值(字符串常量)(如 "abc" )都作为此类的实例实现(对象)。
字符串是常量;它们的值在创建之后不能更改(但凡是你感觉它要变化的时候,其实都是创建了一个新的字符串)。
1. 字符串不变:字符串的值在创建后不能被更改。 String s1 = "abc"; s1 += "d"; System.out.println(s1);
|
创建字符串对象的区别对比
- 通过构造方法创建 通过 new 创建的字符串对象,每一次 new 都会申请一个内存空间,虽然内容相同,但是地址值不同
- 直接赋值方式创建 以“”方式给出的字符串,只要字符序列相同(顺序和大小写),无论在程序代码中出现几次,JVM 都只会建立一个 String 对象,并在字符串池中维护
== 的使用:
1.基本类型: 比较的是具体的数据值是否相同
2.引用类型: 比较的是地址值是否相同

1 2 3 4
| 2. 因为String对象是不可变的,所以它们可以被共享。 String s1 = "abc"; String s2 = "abc";
|

1 2 3 4 5 6 7 8 9
| 3. "abc" 等效于 char[] data={ 'a' , 'b' , 'c' } jdk8及以前: String内部使用的就是字符数组 jdk9及以后: String内部使用的就是字节数组 例如: String str = "abc"; 相当于: char data[] = {'a', 'b', 'c'}; String str = new String(data);
|
2.2 String类的构造方法
注意:
String这个类比较特殊, 打印其对象名的时候, 不会出现内存地址
而是该对象所记录的真实内容. 面向对象-继承, Object类
当我们需要把字节数组或字符数组转字符串的时候,才会考虑使用构造方法,其他情况不建议使用,因为每new1次都会在内存中产生新的地址值,浪费空间;
| 构造方法 |
说明 |
new String() |
创建空白字符串,等价于 "" |
new String(char[] chs) |
将字符数组转为字符串 |
new String(byte[] bs) |
将字节数组转为字符串 |
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
public class Demo01String { public static void main(String[] args) { String s1 = new String(); System.out.println("Hello"+s1+"World");
char[] chs = {'a','b','c'}; String s2 = new String(chs); System.out.println(s2);
byte[] bs = {97,98,99}; String s3 = new String(bs); System.out.println(s3);
String s4 = "abc"; System.out.println(s4); } }
|
2.4 创建字符串对象的区别对比
2.5 字符串的比较
- == 比较基本数据类型:比较的是具体的值
- == 比较引用数据类型:比较的是对象地址值
String类 : public boolean equals(String s) 比较两个字符串内容是否相同、区分大小写
代码 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Demo1Equals { public static void main(String[] args) { String s1 = "abc"; String s2 = "ABC"; String s3 = "abc";
System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3));
System.out.println(s1.equalsIgnoreCase(s2)); } }
|
2.6 遍历字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import java.util.Scanner;
public class Test2 {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入:"); String s = sc.nextLine(); for(int i = 0; i < s.length(); i++){ char c = s.charAt(i); System.out.println(c); } } }
|
2.7 统计字符次数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import java.util.Scanner;
public class Test3 {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入:"); String s = sc.nextLine(); char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) { System.out.println(chars[i]); } } }
|
2.8 手机号屏蔽-字符串截取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import java.util.Scanner;
public class Test5 {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入手机号:"); String telString = sc.nextLine(); String start = telString.substring(0,3); String end = telString.substring(7); System.out.println(start + "****" + end); } }
|
2.9 敏感词替换-字符串替换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import java.util.Scanner;
public class Test6 {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入:"); String s = sc.nextLine(); String result = s.replace("TMD","***"); System.out.println(result); } }
|
2.10 切割字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import com.itheima.domain.Student;
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入学生信息:"); String stuInfo = sc.nextLine(); String[] sArr = stuInfo.split(",");
Student stu = new Student(sArr[0],sArr[1]);
System.out.println(stu.getName() + "..." + stu.getAge()); } }
|
2.11 String的常用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
public class Demo03PanDuan { public static void main(String[] args) { char[] chs = {'a','b','c'}; String s1 = "abc"; String s2 = new String(chs); String s3 = new String("abc"); String s4 = "Abc";
System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println(s2.equals(s3)); System.out.println(s1.equals(s4));
System.out.println(s1.equalsIgnoreCase(s2)); System.out.println(s1.equalsIgnoreCase(s3)); System.out.println(s2.equalsIgnoreCase(s3)); System.out.println(s1.equalsIgnoreCase(s4));
String s5 = "Hello World"; System.out.println(s5.contains("Hello")); System.out.println(s5.contains("hello")); System.out.println(s5.contains(" World")); System.out.println(s5.contains(" world")); } }
public class Demo05HuoQu { public static void main(String[] args) { String s1 = "Hello World"; System.out.println(s1.length()); System.out.println("".length());
String s2 = " ITheima"; String s3 = s1.concat(s2); System.out.println(s1); System.out.println(s2); System.out.println(s3);
System.out.println(s1.charAt(6)); System.out.println(s1.charAt(s1.length() - 1));
System.out.println("小字符串World在大字符串Hello World中出现的索引: "+s1.indexOf("World")); System.out.println("小字符串world在大字符串Hello World中出现的索引: "+s1.indexOf("world"));
String s4 = s1.substring(6, 11); System.out.println(s4);
String s5 = s1.substring(6); System.out.println(s5); System.out.println(s1); } }
public class Demo04StringConvert { public static void main(String[] args) { String s1 = "abc"; char[] chs = s1.toCharArray(); System.out.println("字符串长度: " + s1.length()); System.out.println("字符数组长度: " + chs.length); for (int i = 0; i < chs.length; i++) { System.out.print(chs[i]+" "); } System.out.println("----------------");
byte[] bs = s1.getBytes(); for (int i = 0; i < bs.length; i++) { System.out.print(bs[i]+" "); }
String s2 = "我靠,你他妈有病,你个二货,你个傻叉....";
String s3 = s2.replace("靠", "*"); System.out.println(s3); } }
public class Demo05StringSplit { public static void main(String[] args) { String s1 = "aaa,bbb,ccc,ddd"; String[] strs = s1.split(","); System.out.println("字符串数组长度: "+strs.length); for (int i = 0; i < strs.length; i++) { System.out.println(strs[i]); }
String s2 = "hello world java"; String[] strs2 = s2.split(" "); System.out.println("字符串数组长度: "+strs2.length); for (int i = 0; i < strs2.length; i++) { System.out.println(strs2[i]); } } }
public class Test7 {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入学生信息:"); String stuInfo = sc.nextLine(); String[] sArr = stuInfo.split(",");
Student stu = new Student(sArr[0],sArr[1]);
System.out.println(stu.getName() + "..." + stu.getAge()); } }
|
3 StringBuilder类
3.1 StringBuilder类概述
概述 : StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器,这里的可变指的是 StringBuilder 对象中的内容是可变的
3.2 StringBuilder类和String类的区别
String类: 内容是不可变的 使用String进行字符串的大量拼接时,产生大量垃圾,效率低
StringBuilder类: 内容是可变的 使用StringBuilder字符串的大量拼接时,产生垃圾少,效率高

3.3StringBuilder类的构造方法
| 方法名 |
说明 |
| public StringBuilder() |
创建一个空白可变字符串对象,不含有任何内容 |
| public StringBuilder(String str) |
根据字符串的内容,来创建可变字符串对象 |
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public class Demo01StringBuilder { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); System.out.println(sb); System.out.println(sb.toString());
StringBuilder sb2 = new StringBuilder("HelloWorld"); System.out.println(sb2); System.out.println(sb2.toString()); } }
|
3.4 StringBuilder常用的成员方法
(没有static修饰,必须使用StringBuilder类的对象调用)
添加和反转方法
| 方法名 |
说明 |
| public StringBuilder append(任意类型) |
添加数据,并返回对象本身 |
| public StringBuilder reverse() |
返回相反的字符序列 |
示例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
public class Demo02StringBuilderMethod { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); sb.append(97); sb.append('a'); sb.append("hello"); sb.append(true); System.out.println(sb);
sb.append("hello").append("world").append("java").append(100); System.out.println("sb:" + sb);
sb.reverse(); System.out.println(sb); StringBuilder sb = new StringBuilder("A"); StringBuilder sb2 = sb.append("B"); StringBuilder sb3 = sb2.append("C"); System.out.println(sb); System.out.println(sb2); System.out.println(sb3);
System.out.println(sb == sb2); System.out.println(sb == sb3); System.out.println(sb2 == sb3);
StringBuilder sb4 = new StringBuilder(); sb4.append(100).append('a').append("hello").append("false"); System.out.println(sb4); } }
|
3.5StringBuilder和String相互转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
public class StringBuilderDemo02 { public static void main(String[] args) {
String s = "hello";
StringBuilder sb = new StringBuilder(s);
System.out.println(sb); } }
|
3.6StringBuilder练习-字符串反转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class Demo06ReverseString { public static void main(String[] args) { String ss = "abc";
String result = reverserString(ss);
System.out.println("反转前: "+ss); System.out.println("反转后: "+result); }
public static String reverserString(String str) { StringBuilder sb = new StringBuilder(str);
sb.reverse();
String newStr = sb.toString();
return newStr; }
public static String reverserString2(String str) { return new StringBuilder(str).reverse().toString(); } }
|
3.7 StringBuilder拼接字符串案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| public class StringBuilderTest01 { public static void main(String[] args) { int[] arr = {1, 2, 3};
String s = arrayToString(arr);
System.out.println("s:" + s);
}
public static String arrayToString(int[] arr) { StringBuilder sb = new StringBuilder();
sb.append("[");
for(int i=0; i<arr.length; i++) { if(i == arr.length-1) { sb.append(arr[i]); } else { sb.append(arr[i]).append(", "); } }
sb.append("]");
String s = sb.toString();
return s; } }
|
4、StringJoiner - 字符串连接器
StringJoiner是Java 8引入的java.util包中的类,专门用于构建由分隔符分隔的字符序列,并可选择性地添加前缀和后缀。
为什么使用?
- 简化字符串拼接:特别是需要添加分隔符的场景
- 避免手动处理分隔符:自动处理元素间的分隔,不会出现多余的或缺失的分隔符
- 代码更清晰:相比StringBuilder或String的+操作,代码意图更明确
- 支持前缀后缀:方便构建格式化的字符串(如JSON数组、SQL IN语句等)
如何使用?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import java.util.StringJoiner;
public class StringJoinerDemo { public static void main(String[] args) { StringJoiner joiner1 = new StringJoiner(", "); joiner1.add("Java"); joiner1.add("Python"); joiner1.add("C++"); System.out.println(joiner1.toString());
StringJoiner joiner2 = new StringJoiner(", ", "[", "]"); joiner2.add("Apple"); joiner2.add("Banana"); joiner2.add("Orange"); System.out.println(joiner2.toString());
StringJoiner joiner3 = new StringJoiner(" | ") .add("Red") .add("Green") .add("Blue"); System.out.println(joiner3.toString());
StringJoiner joiner4 = new StringJoiner(", "); System.out.println(joiner4.toString());
StringJoiner joiner5 = new StringJoiner(", ", "{", "}"); joiner5.setEmptyValue("{}"); System.out.println(joiner5.toString()); } }
|
对比其他字符串拼接方式
| 方法 |
优点 |
缺点 |
适用场景 |
| StringJoiner |
自动处理分隔符,支持前后缀,代码清晰 |
Java 8+,略微性能开销 |
格式化列表、路径拼接、SQL语句等 |
| StringBuilder |
性能高,功能灵活 |
需要手动处理分隔符和格式 |
循环拼接、性能敏感场景 |
| String + 操作符 |
简单直接 |
每次+都创建新对象,性能差 |
简单少量拼接 |