String字符串定义

1. String是什么,可以做什么?
答:
String**代表字符串,可以用来创建对象封装字符串数据,并对其进行处理。

2.String类创建对象封装字符串数据的方式有几种?
方式一: 直接使用双引号
“…”

方式二:new String类,调用构造器初始化字符串对象。

基础Code

1
2
3
4
5
6
7
8
9
10
11
12
13
String name = "小明";
System.out.println(name);

String s1 = new String("小李");
System.out.println(s1);

char[] char1 = {'a', '挨', '踢'};
String s2 = new String(char1);
System.out.println(s2);

byte[] by = {99, 65, 102}; // 注意a = 65 A = 97
String s3 = new String(by);
System.out.println(s3);

String类的常用方法

  • 提取字符串中某个索引位置处的字符s.charAt(索引)
  • 获取字符串的长度s.length()
  • 把字符串转换成数组s.toCharArray()
  • 比较字符串s.equals(s1)
  • 忽略大小写比较字符串内容s.equalsIgnoreCase(s1)
  • 截取字符串内容s.substring(0, 5); 取s1的字串0,到 4 的位置
  • 把字符串中的某个内容替换成新内容,并返回新的字符串对象s.replace("挨踢","IT");
  • 判断字符串中是否包含某个关键字s.contains("IT")
  • 判断字符串是否以某个字符串开头s.startsWith("好")
  • 把字符串按照某个指定内容分割成多个字符串s.split("")
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
String s1 = "学挨踢成为程序猿";
System.out.println(s1.length()); //获取字符串的长度
char ch = s1.charAt(1); // 提取字符串中某个索引位置处的字符
System.out.println(ch);
// 字符串的遍历
for (int i = 0; i < s1.length(); i++) {
char c = s1.charAt(i);
System.out.print(c + "\t"); // 一定是双引号啊
}
System.out.println();
// 把字符串转换成数组在进行遍历
char[] c1 = s1.toCharArray(); // 将字符串转为数组的方法
for (int i = 0; i < c1.length; i++) {
System.out.print(c1[i] + "\t");
}
System.out.println();
// 判断字符串内容,内容一样就返回true
String s2 = "小李"; // 和下面的字符串创建方法是一样的
String s3 = new String("小李");
System.out.println(s3.equals(s2)); // 比较字符串的方法
//忽略大小写比较字符串内容
System.out.println(s2.equalsIgnoreCase(s3));
// 截取字符串内容 (包前不包后的-取头不取尾--跟python差不多
String s4 = s1.substring(0, 5); // 取s1的字串0,到 4 的位置
System.out.println(s4); // s1.substring(4)从当前索引4位置一直截取到字符串的末尾
//把字符串中的某个内容替换成新内容,并返回新的字符串对象给我们
String s5 = s1.replace("挨踢","IT");
System.out.println(s5);
// 判断字符串中是否包含某个关键字
System.out.println(s5.contains("IT"));
// 判断字符串是否以某个字符串开头
System.out.println(s5.startsWith("好"));
// 把字符串按照某个指定内容分割成多个字符串,放到一个字符串数组中返回给我们
String[] result = s1.split("");
for (int i = 0; i < result.length; i++) {
System.out.print(result[i] + "\t");
}

String案例一:用户登录案例

需求

设计用户登陆页面
1.首先,从登录界面上可以得出,需要让用户输入登录名和密码
2.设计一个登录方法,对用户名和密码进行校验
3.调用登录方法,根据方法的返回结果,判断登录是否成功。
4.如果登录失败,循环登录3次,结束循环;如果登录成功,跳出循环;

1
2
3
4
5
6
7
8
9
10
for (int i = 0; i < 3; i++) {
Scanner sc = new Scanner(System.in);
System.out.println("请您输入用户名:");
String username = sc.next(); // 接收用户的输入
System.out.println("请您输入密码:");
String pwd = sc.next();
boolean if_log = log_in(username, pwd);
if (if_log == false){
System.out.println("登陆失败,请您再次输入");
}else System.out.println("登陆成功");

*String案例二:随机产生验证码*※

需求

需求:实现随机产生验证码,可以是数字字母大小写
1.首先,设计一个方法,该方法接收一个整型参数,最终要返回对应位数的随机验证码。
2.方法内定义2个字符串变量:
1个用来记住生成的验证码,1个用来记住要用到的全部字符。
3.定义for循环控制生成多少位随机字符
4.每次得到一个字符范围内的随机索引
5.根据索引提取该字符,把该字符交给code变量连接起
6.循环结束后,在循环外返回code即可。
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
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Random;
import java.util.Scanner;
public class Identify_Code {
public static void main(String[] args) {
System.out.println("请输入验证码的位数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String code = creatcode(n);
System.out.println("验证码为:" + code);
}
public static String creatcode(int n){
//定义一个for循环,循环n次,依次生成随机字符
Random r = new Random();
String code = ""; // 定义要生成的字符,初始为空
for (int i = 0; i < n; i++){
//通过random来确定到底来生成什么类型的字符
int type = r.nextInt(3); //type的取值为0-2
switch (type){
case 0:
//生成大写字母A 65 Z 65+25
char ch = (char)(r.nextInt(26) + 65); //0-25)+65
code = code + ch;
break;
case 1:
//生成小写字母a 97 z 97+25
char ch1 = (char)(r.nextInt(26) + 97); //0-25)+97
code += ch1;
break;
case 2:
//生成数字
int num = r.nextInt(10); //生成0-9之间的数字
code += num;
break;
}
}
return code;
}
}