目录
6、String 案例练习(生成验证码、模拟登录、手机号屏蔽)
API的概述
*API全称为应用程序编程接口
*是java中写好的功能代码 可以直接调用
1、String的概述
1.1 String类定义的变量用于存储字符串 并且String提供了许多对字符串操作的功能 可以直接使用
1.2 为字符串类型 可以定义字符串变量指向字符串对象
1.3 JAVA程序中所有加双引号例如:"字符串"都为字符串对象
1.4 *String的特点
被称为不可变字符串类型 在创建之后就不可更改
2、创建字符串对象
2.1 方法1:直接使用双引号定义
//例如:
String name = "皮卡丘";
2.2 方法2:通过String构造器创建对象
| 常用的典型构造器 | 说明 |
| public String() | 创建一个空白字符串对象 |
| public String(String original) | 根据传入内容 创建字符串对象 |
| public String(char[] chs) | 根据字符串数组内容 创建字符串对象 |
| public String(byte[] chs) | 根据字节数组内容 创建字符串对象 |
public static void main(String[] args) {
//public String() 创建一个空白字符串对象
String s1 = new String();
s1 = "我爱中国";
System.out.println(s1);
System.out.println("-----------------------------------------");
//public String(String original) 根据传入内容 创建字符串对象
String s2 = new String("我爱伟大的祖国");
System.out.println(s2);
System.out.println("-----------------------------------------");
//public String(char[] chs) 根据字符串数组内容 创建字符串对象
char[] chars= {'我','是','中','国','人'}; //遍历拼接字符串并返回
String s3 = new String(chars);
System.out.println(s3);
System.out.println("-----------------------------------------");
//public String(byte[] chs) 根据字节数组内容 创建字符串对象
byte[] bytes = {97,98,99,65,66}; //根据编码 返回对应字符
String s4 = new String(bytes);
System.out.println(s4);
System.out.println("-----------------------------------------");
}
执行效果:
我爱中国
-----------------------------------------
我爱伟大的祖国
-----------------------------------------
我是中国人
-----------------------------------------
abcAB
-----------------------------------------
3、对象内存中的区别
*以" " 双引号方式给出的字符串对象 在字符串常量池中存储 相同内容会在其中存储一份
*通过构造器new对象 每new一次都会产生一个新的对象 放在堆内存中
public static void main(String[] args) {
String s1 = "你好呀";
String s2 = "你好呀";
//此时 s1 = s2 的返回结果为true
char[] chars= {'我','是','中','国','人'};
String s3 = new String(chars);
String s4 = new String(chars);
//此时 s3 = s4 的返回结果为false
}
4、String常用API和字符串内容比较
4.1 String提供equals比较 API
因为 等等于号( == )对比的是字符串存储地址 所有不适用这种方法比较字符串(基本数据类型比较时使用)
| 方法 | 描述 |
| public boolean equals(Object anObject) | 将字符串指定对象比较 判断字符串是否完全一致 |
| public boolean equalsIgnoreCase(String anotherString) | 将字符串指定对象比较 忽略大小写比较 判断字符串是否一致 |
//例如:
public static void main(String[] args) {
//设置用户名和密码
String userName = "abcd";
String userPassword = "1234";
//扫描器输入用户和密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = sc.next();
System.out.println("请输入用户密码:");
String password = sc.next();
//判断输入的用户和密码是否正确
if(userName.equals(name) && userPassword.equals(password)){
System.out.println("输入正确 登录成功");
}else {
System.out.println("用户或密码错误");
}
}

equalsIgnoreCase忽略大小写内容的API一般用于验证码等相似第逻辑业务
4.2 其他常用的StringAPI
| 构造方法 | 说明 |
| public int length | 返回字符串长度 |
| char charAt(int index) | 返回指定索引处的 char 值。 |
| char tocharArray(): | 将当前字符串转换成字符数组返回 |
| String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取 得到新字符串(抱前不包后) |
| String substring(int beginIndex) | 从传入的索引处截取 截取到末尾 的到新的字符串 |
| String replace(CharSequence target,CharSequence replacement) | 使用新值 将字符串中的旧值替换得到新的字符串 |
| String[] split(String regex) | 根据传入的规则切割字符串 得到字符串数组返回 |
public static void main(String[] args) {
//public int length 返回字符串长度
String uu = "雷霆嘎巴";
System.out.println(uu.length());
执行结果:
4
//char charAt(int index) 返回指定索引处的 char 值。
char c =uu.charAt(1);
System.out.println(c);
//可以使用该方法遍历字符串每一项
for (int i = 0; i < uu.length(); i++) {
char cc = uu.charAt(i);
System.out.println(cc);
}
执行结果:
霆
雷
霆
嘎
巴
//char tocharArray(): 将当前字符串转换成字符数组返回
char[] chars = uu.toCharArray();
//遍历循环
for (int i = 0; i <uu.length() ; i++) {
char ccc = uu.charAt(i);
System.out.println(ccc);
}
执行结果:
雷
霆
嘎
巴
//String substring(int beginIndex, int endIndex) 根据开始和结束索引进行截取 得到新字符串(抱前不包后)
String name = "菠萝吹雪笑哈哈";
System.out.println(name.substring(0, 4));
执行结果:
菠萝吹雪
//String substring(int beginIndex) 从传入的索引处截取 截取到末尾 的到新的字符串
System.out.println(name.substring(4));
执行结果:
笑哈哈
//String replace(CharSequence target,CharSequence replacement) 使用新值 将字符串中的旧值替换得到新的字符串
String name1 = "xxx真了不起";
String nee = name1.replace("xxx", "小果丁");
System.out.println(nee);
执行结果:
小果丁真了不起
//boolean contains(CharSequence s) 判断是否包含在其中
System.out.println(name1.contains("真了不起"));
执行结果:
true
//boolean startsWith(String prefix) 判断是否为首字开始
System.out.println(name1.startsWith("xxx"));
执行结果:
true
//String[] split(String regex) 根据传入的规则切割字符串 得到字符串数组返回
String name2 = "小猪,小狗,小猫,小牛";
String[] nnn = name2.split(",");
//然后遍历列表
for (int i = 0; i <nnn.length ; i++) {
System.out.println(nnn[i]);
执行结果:
小猪
小狗
小猫
小牛
}
}
5.、String的API全览
| 构造方法 | 说明 |
| public int length | 返回字符串长度 |
| public char charAt(int index) | 获取某个索引处的字符 |
| char charAt(int index) | 返回指定索引处的 char 值。 |
| int compareTo(Object o) | 把这个字符串和另一个对象比较。 |
| int compareTo(String anotherString) | 按字典顺序比较两个字符串。 |
| int compareToIgnoreCase(String str) | 按字典顺序比较两个字符串,不考虑大小写。 |
| String concat(String str) | 将指定字符串连接到此字符串的结尾。 |
| boolean contentEquals(StringBuffer sb) | 当且仅当字符串与指定的StringBuffer有相同顺序的字符时候返回真。 |
| static String copyValueOf(char[] data) | 返回指定数组中表示该字符序列的 String。 |
| static String copyValueOf(char[] data, int offset, int count) | 返回指定数组中表示该字符序列的 String。 |
| boolean endsWith(String suffix) | 测试此字符串是否以指定的后缀结束。 |
| boolean equals(Object anObject) | 将此字符串与指定的对象比较。 |
| boolean equalsIgnoreCase(String anotherString) | 将此 String 与另一个 String 比较,不考虑大小写。 |
| byte[] getBytes() | 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 |
| byte[] getBytes(String charsetName) | 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 |
| void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | 将字符从此字符串复制到目标字符数组。 |
| int hashCode() | 返回此字符串的哈希码。 |
| int indexOf(int ch) | 返回指定字符在此字符串中第一次出现处的索引。 |
| int indexOf(int ch, int fromIndex) | 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。 |
| int indexOf(String str) | 返回指定子字符串在此字符串中第一次出现处的索引 |
| int indexOf(String str, int fromIndex) | 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。 |
| String intern() | 返回字符串对象的规范化表示形式。 |
| int lastIndexOf(int ch) | 返回指定字符在此字符串中最后一次出现处的索引。 |
| int lastIndexOf(int ch, int fromIndex) | 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。 |
| int lastIndexOf(String str) | 返回指定子字符串在此字符串中最右边出现处的索引。 |
| int lastIndexOf(String str, int fromIndex) | 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。 |
| int length() | 返回此字符串的长度。 |
| boolean matches(String regex) | 告知此字符串是否匹配给定的正则表达式。 |
| boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) | 测试两个字符串区域是否相等。 |
| boolean regionMatches(int toffset, String other, int ooffset, int len) | 测试两个字符串区域是否相等。 |
| String replace(char oldChar, char newChar) | 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 |
| String replaceAll(String regex, String replacement) | 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 |
| String replaceFirst(String regex, String replacement) | 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。 |
| String[] split(String regex) | 根据给定正则表达式的匹配拆分此字符串。 |
| String[] split(String regex, int limit) | 根据匹配给定的正则表达式来拆分此字符串。 |
| boolean startsWith(String prefix) | 测试此字符串是否以指定的前缀开始。 |
| boolean startsWith(String prefix, int toffset) | 测试此字符串从指定索引开始的子字符串是否以指定前缀开始。 |
| CharSequence subSequence(int beginIndex, int endIndex) | 返回一个新的字符序列,它是此序列的一个子序列。 |
| String substring(int beginIndex) | 返回一个新的字符串,它是此字符串的一个子字符串。 |
| String substring(int beginIndex, int endIndex) | 返回一个新字符串,它是此字符串的一个子字符串。 |
| char[] toCharArray() | 将此字符串转换为一个新的字符数组。 |
| String toLowerCase() | 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。 |
| String toLowerCase(Locale locale) | 使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。 |
| String toString() | 返回此对象本身(它已经是一个字符串!) |
| String toUpperCase() | 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。 |
| String toUpperCase(Locale locale) | 使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。 |
| String trim() | 返回字符串的副本,忽略前导空白和尾部空白。 |
| static String valueOf(primitive data type x) | 返回给定data type类型x参数的字符串表示形式。 |
| contains(CharSequence chars) | 判断是否包含指定的字符系列。 |
| isEmpty() | 判断字符串是否为空。 |
6、String 案例练习(生成验证码、模拟登录、手机号屏蔽)
6.1验证码功能
随机产生一个5位数的验证码 每位可能是数字、大写字母、小写字母
思路:1.定义一个String型变量存储a-z、A-z、0-9的全部字符
2.循环5次 随机一个范围的索引 获取对应字符连接起来
public static void main(String[] args) {
//定义字符串信息
String data = "abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ0123456789";
//定义一个空字符串来存储
String fio = "";
//循环5次 每次生成一个随机的索引 提取对应字符然后连接起来
Random r = new Random();
for (int i = 0; i < 5; i++) {
//随机索引
int ind = r.nextInt(data.length());
char c = data.charAt(ind);
fio += c;
}
//输出结果
System.out.println(fio);
}
6.2模拟用户登录
模拟用户登录功能 只有三次机会
思路:1.定义好正确的登录名称和密码
2.使用循环控制输入三次 判断是否登录成功 成功则不再继续登录 失败提示继续登录
blic static void main(String[] args) {
//定义正确登录名称和密码
String UserName = "abcde";
String UserPassword = "123456";
//定义扫描器输入数据
Scanner s = new Scanner(System.in);
//定义循环 输入三次
for (int i = 0; i <= 3; i++) {
System.out.println("请输入用户名:");
String loginName = s.next();
String loginPassword = s.next();
//判断登录是否成功
if (UserName.equals(loginName)) {
//判断密码是否正确
if (UserPassword.equals(loginPassword)) {
System.out.println("登录成功!");
break;
} else {
System.out.println("密码不正确" + "还有" + (3 - i) + "次机会");
}
} else {
System.out.println("用户名或密码输入有误!"+ "还有" + (3 - i) + "次机会");
}
}
}
6.3手机号码的屏蔽
以字符串形式接收一个手机号 将中间四位号码屏蔽
如:158****6845
思路:1.键盘输入号码(使用Scanner实现)
2.截取字符串前三位 截取字符串后四位
3.将截取字符串中间使用****拼接 输出
public static void main(String[] args) {
//键盘录入手机号码
Scanner sc = new Scanner(System.in);
System.out.println("输入你的手机号码:");
String num = sc.next();
//截取号码前3为和后4位
String bofore = num.substring(0,3);
String end = num.substring(7);//从索引7截取到末尾
//字符串拼接
String outcome = bofore + "****" +end;
//输出结果
System.out.println(outcome);
}

286

被折叠的 条评论
为什么被折叠?



