1、String概述
- java.lang.String类代表字符串,Java程序中的所有字符串文字(例如“abc”)都为此类的对象。
1.1、String的注意点
- 字符串的内容是不会发生改变的,它的对象在创建后不能被更改。
1.2、总结
- String是Java定义好的一个类。定义在java.lang包中,所以使用的时候不需要导包。
- Java程序中的所有字符串文字都被实为此类的对象。
- 字符串不可变,它们的值在创建后不能被更改。
2、String构造方法代码实现和内存分析
2.1、创建String对象的两种方式
- 直接赋值
package stringdemo;
public class StringDemo1 {
public static void main(String[] args) {
String s1 = "abc";
System.out.println(s1);
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=21864:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringDemo1
abc
Process finished with exit code 0
- new
构造方法 | 说明 |
public String() | 创建空白字符串,不含任何内容 |
public String(String original) | 根据传入的字符串,创建字符串对象 |
public String(char[ ] chs) | 根据字符数组,创建字符串对象 |
public String(byte[ ] chs) | 根据字节数组,创建字符串对象 |
package stringdemo;
public class StringDemo1 {
public static void main(String[] args) {
String s2 = new String();
System.out.println("@" + s2 + "!");//@!
String s3 = new String("abc");
System.out.println(s3);//abc
// 应用场景:修改字符串的内容。abcd --> Qbcd
char[] chs = {'a', 'b', 'c', 'd'};
String s4 = new String(chs);
System.out.println(s4);//abcd
// 应用场景:以后在网络当中传输的数据其实都是字节信息
// 我们一般要把字节信息进行转换,转成字符串,此时就要用到这个构造了。
byte[] bytes = {97, 98, 99, 100};
String s5 = new String(bytes);
System.out.println(s5);//abcd
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=2016:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringDemo1
@!
abc
abcd
abcd
Process finished with exit code 0
2.2、String内存分析,内存模型
2.2.1、直接赋值内存分析
- 注意:StringTable(串池)在JDK7版本开始从方法区挪到了堆内存
- 当使用双引号直接赋值时,系统会检查字符串在串池中是否存在。不存在:创建新的,存在:复用
2.2.2、手动new内存图解析
3、字符串的比较
==号比的到底是什么? |
---|
基本数据类型 | 引用数据类型 |
基本数据类型比较的是数据值 | 引用数据类型比较的是地址值 |
| |
字符串的比较 |
- boolean equals方法(要比较的字符串)
|
|
- boolean equalsIgnoreCase(要比较的字符串)
|
|
package stringdemo;
import java.util.Scanner;
public class StringDemo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String str1 = sc.next();//是new出来的
String str2 = "abc";
System.out.println(str1 == str2);//false
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=4197:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringDemo1
请输入一个字符串
abc
false
Process finished with exit code 0
- 查看next();方法的源码发现键盘输入的字符串是new出来的。
4、练习-用户登录
- 已知正确的用户名和密码,请用程序实现模拟用户登录。总共给三次机会,登录之后,给出相应的提示。
-
package stringdemo;
import java.util.Scanner;
public class StringDemo1 {
public static void main(String[] args) {
String rightUsername = "zhangsan";
String rightPassword = "123456";
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("请输入用户名");
String username = sc.next();
System.out.println("请输入密码");
String password = sc.next();
if (username.equals(rightUsername) && password.equals(rightPassword)) {
System.out.println("用户登录成功");
break;
} else {
if (i == 2) {
System.out.println("账号" + username + "被锁定,请联系管理员客服:");
}
System.out.println("用户登录失败,用户名或密码有误,您还剩下" + (2 - i) + "次机会");
}
}
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=5568:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringDemo1
请输入用户名
z
请输入密码
z
用户登录失败,用户名或密码有误,您还剩下2次机会
请输入用户名
z
请输入密码
z
用户登录失败,用户名或密码有误,您还剩下1次机会
请输入用户名
z
请输入密码
z
账号z被锁定,请联系管理员客服:
用户登录失败,用户名或密码有误,您还剩下0次机会
z
Process finished with exit code 0
5、练习-遍历字符串和统计字符个数
5.1、遍历字符串
- 需求:键盘录入一个字符串,使用程序实现在控制台遍历该字符串
- public char chatAt(int index):
|
|
|
|
|
|
|
|
package stringdemo;
import java.util.Scanner;
public class StringTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串");
String next = sc.next();
for (int i = 0; i < next.length(); i++) {
char c = next.charAt(i);
System.out.println(c);
}
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=31884:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringTest
请输入字符串
abcd
a
b
c
d
Process finished with exit code 0
5.2、统计字符次数
- 键盘录入一个字符串,统计该字符串中大写字母字符,小写字符字符,数字字符出现的次数(不考虑其他字符)
package stringdemo;
import java.util.Scanner;
public class StringTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串");
String next = sc.next();
int capital = 0;
int smallLetter = 0;
int numberCount = 0;
for (int i = 0; i < next.length(); i++) {
char c = next.charAt(i);
if (c >= 'A' && c <= 'Z') {
capital++;
}
if (c >= 'a' && c <= 'z') {
smallLetter++;
}
if (c >= 48 && c <= 57) {
numberCount++;
}
// System.out.println(c);
}
System.out.println("大写字母有:" + capital + "个");
System.out.println("小写字母有:" + smallLetter + "个");
System.out.println("数字有:" + numberCount + "个");
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=33003:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringTest
请输入字符串
AZRTazqwer39473
大写字母有:4个
小写字母有:6个
数字有:5个
Process finished with exit code 0
6、练习-拼接字符串和反转
6.1、拼接字符串
- 定义一个方法,把int数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,并在控制台输出结果。
package stringdemo;
public class StringTest {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
String s = arrToString(arr);
System.out.println(s);
}
public static String arrToString(int[] arr) {
if (arr == null) {
return "";
}
if (arr.length == 0) {
return "[]";
}
String result = "[";
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
result = result + arr[i];
} else {
result = result + arr[i] + ", ";
}
}
result = result + "]";
return result;
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=33811:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringTest
[1, 2, 3]
Process finished with exit code 0
6.2、字符串反转
- 定义一个方法,实现字符串反转。键盘录入一个字符串,调用该方法后,在控制台输出结果,例如,键盘录入abc,输出结果cba
package stringdemo;
import java.util.Scanner;
public class StringTest {
public static void main(String[] args) {
System.out.println(reverser());
}
public static String reverser() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串");
String str = sc.next();
String result = "";
for (int length = str.length() - 1; length >= 0; length--) {
char c = str.charAt(length);
result = result + c;
}
return result;
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=34637:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringTest
请输入字符串
abc
cba
Process finished with exit code 0
7、较难练习-金额转换
package stringdemo;
import java.util.Scanner;
public class StringTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int money;
while (true) {
System.out.println("请输入一个金额");
money = sc.nextInt();
if (money >= 0 && money <= 9999999) {
break;
} else {
System.out.println("金额无效");
}
}
String moneyStr = "";
while (true) {
int ge = money % 10;
String capitalNumber = getCapitalNumber(ge);
moneyStr = capitalNumber + moneyStr;
money = money / 10;
if (money == 0) {
break;
}
}
int count = 7 - moneyStr.length();
for (int i = 0; i < count; i++) {
moneyStr = "零" + moneyStr;
}
System.out.println(moneyStr);
String[] arr = {"佰", "拾", "万", "仟", "佰", "拾", "元"};
String result = "";
for (int i = 0; i < moneyStr.length(); i++) {
char c = moneyStr.charAt(i);
result = result + c + arr[i];
}
System.out.println(result);
}
public static String getCapitalNumber(int number) {
String[] arr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
return arr[number];
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=2152:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringTest
请输入一个金额
2135
零零零贰壹叁伍
零佰零拾零万贰仟壹佰叁拾伍元
Process finished with exit code 0
8、练习-手机号屏蔽-身份证号码信息查看-游戏骂人敏感词替换
- String substring (int beginIndex,int endIndex)
|
- 截取(注意点:包头不包尾,包左不包右,只有返回值才是截取的小串)
|
- String substring(int beginIndex)
|
|
8.1、手机号屏蔽
package stringdemo;
public class StringTest {
public static void main(String[] args) {
String phoneNumber = "13112349468";
String start = phoneNumber.substring(0, 3);
String end = phoneNumber.substring(7);
String result = start + "****" + end;
System.out.println(result);
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=3056:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringTest
131****9468
Process finished with exit code 0
8.2、身份证号码信息查看
package stringdemo;
public class StringTest {
public static void main(String[] args) {
String id = "321281202308131234";
String year = id.substring(6, 10);
String month = id.substring(10, 12);
String day = id.substring(12, 14);
System.out.println("人物信息为:");
System.out.println("出生年月日:" + year + "年" + month + "月" + day + "日");
char gender = id.charAt(16);
//利用ASCII码表
//'0' --> 48 48-48=0
//'1' --> 49 49-48=1
//'2' --> 50 50-48=2
//'3' --> 51 51-48=3
//'4' --> 52 52-48=4
//'5' --> 53 53-48=5
//'6' --> 54 54-48=6
//'7' --> 55 55-48=7
//'8' --> 56 56-48=8
//'9' --> 57 57-48=9
int num = gender - 48;
if (num % 2 == 0) {
System.out.println("性别为:女");
} else {
System.out.println("性别为:男");
}
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=2804:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringTest
人物信息为:
出生年月日:2023年08月13日
性别为:男
Process finished with exit code 0
8.3、敏感词替换
package stringdemo;
public class StringTest {
public static void main(String[] args) {
String talk = "你玩的真好,SB以后不要再玩了,TMD";
String[] arr = {"TMD", "CMD", "SB", "MLGB"};
for (int i = 0; i < arr.length; i++) {
talk = talk.replace(arr[i], "***");
}
System.out.println(talk);
}
}
D:\Java\jdk-17\bin\java.exe "-javaagent:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\lib\idea_rt.jar=3940:D:\BaiduNetdiskDownload\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath F:\IdeaProjects\JavaSenior\out\production\Exception stringdemo.StringTest
你玩的真好,***以后不要再玩了,***
Process finished with exit code 0