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}; 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();
String s2 = "小李"; String s3 = new String("小李"); System.out.println(s3.equals(s2));
System.out.println(s2.equalsIgnoreCase(s3));
String s4 = s1.substring(0, 5); System.out.println(s4);
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){ Random r = new Random(); String code = ""; for (int i = 0; i < n; i++){ int type = r.nextInt(3); switch (type){ case 0: char ch = (char)(r.nextInt(26) + 65); code = code + ch; break; case 1: char ch1 = (char)(r.nextInt(26) + 97); code += ch1; break; case 2: int num = r.nextInt(10); code += num; break; } } return code; } }
|