1 package day8.learn_String;
2
3 /*
4 2 String
5
6 2.1 String类概述
7 String 类代表字符串,Java 程序中的所有字符串文字(例如“abc”)都被实现为此类的实例。
8 也就是说,Java 程序中所有的双引号字符串,都是 String 类的对象。
9 String 类在 java.lang 包下,所以使用的时候不需要导包!
10
11 2.2 String类的特点
12 字符串不可变,它们的值在创建后不能被更改
13 虽然 String 的值是不可变的,但是它们可以被共享
14 字符串效果上相当于字符数组( char[] ),但是底层原理是字节数组( byte[] )
15
16 2.3 String对象的常用创建方法
17 (1)通过构造方法创建
18 通过 new 创建的字符串对象,每一次 new 都会申请一个内存空间,虽然内容相同,但是地址值不同。
19 例如:
20 public String() 创建一个空白字符串对象,不含有任何内容
21 public String(char[] chs) 根据字符数组的内容,来创建字符串对象
22 public String(byte[] bys) 根据字节数组的内容,来创建字符串对象
23 (2)直接赋值方式创建(推荐)
24 以“”方式给出的字符串,只要字符序列相同(顺序和大小写),
25 无论在程序代码中出现几次,JVM 都只会建立一个 String 对象,并在字符串池中维护。
26 例如:
27 String s = “abc”; 直接赋值的方式创建字符串对象,内容就是abc
28
29 2.4 字符串的比较
30 (1)==号的作用
31 比较基本数据类型:比较的是具体的值
32 比较引用数据类型:比较的是对象地址值
33 (2)equals方法的作用
34 public boolean equals(String s) 比较两个字符串对象内容是否相同、区分大小写
35 */
36
37 public class demo1 {
38 public static void main(String[] args) {
39 String s1 = new String();
40 System.out.println("s1:" + s1);
41
42 char[] chs = {'a', 'b', 'c'};
43 String s2 = new String(chs);
44 System.out.println("s2:" + s2);
45
46 byte[] bys = {97, 98, 99}; //ASCII码
47 String s3 = new String(bys);
48 System.out.println("s3:" + s3); //abc
49
50 String s4 = "abc";
51 System.out.println("s4:" + s4);
52
53 String s5 = "abc";
54 System.out.println(s5 == s4); //true 地址相同
55
56 String s6 = new String(chs);
57 System.out.println(s6 == s2); //false 地址不同
58
59 System.out.println(s6.equals(s2));
60 System.out.println(s5.equals(s5));
61 System.out.println(s6.equals(s5));
62 //内容“abc”都相同
63 }
64 }
1 package day8.learn_String;
2
3 import java.util.Scanner;
4
5 /*
6 2.5 案例-用户登录
7 已知用户名和密码,请用程序实现模拟用户登录。总共给三次机会,登录之后,给出相应的提示
8
9 2.6 案例-遍历字符串
10 键盘录入一个字符串,使用程序实现在控制台遍历该字符串
11
12 2.7 案例-统计字符次数
13 键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数(不考虑其他字符)
14
15 2.8 案例-字符串拼接
16 定义一个方法,把 int 数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,
17 并在控制台输出结果。例如,数组为 int[] arr = {1,2,3}; ,执行方法后的输出结果为:[1, 2, 3]
18
19 2.9 案例-字符串反转
20 定义一个方法,实现字符串反转。键盘录入一个字符串,调用该方法后,在控制台输出结果
21 例如,键盘录入 abc,输出结果 cba
22 */
23
24 public class demo2 {
25 public static void main(String[] args) {
26 // stringTest25();
27 // stringTest26();
28 // stringTest27();
29 // stringTest28();
30 stringTest29();
31 }
32
33 public static void stringTest25(){
34 String username = "hello";
35 String password = "java";
36
37 for(int i=0; i<3; i++){
38 Scanner sc = new Scanner(System.in);
39 System.out.print("请输入用户名:");
40 String inputName = sc.nextLine();
41 System.out.print("请输入密码:");
42 String inputPswd = sc.nextLine();
43 if(inputName.equals(username) && inputPswd.equals(password)){
44 System.out.println("登录成功");
45 break;
46 }else {
47 if(2-i == 0){
48 System.out.println("你的账户被冻结,请与管理员联系");
49 }else {
50 System.out.println("登录失败,你还有" + (2-i) + "次登录机会");
51 }
52 }
53 }
54 }
55
56 public static void stringTest26(){
57 Scanner sc = new Scanner(System.in);
58 System.out.println("请输入一个字符串:");
59 String str = sc.nextLine();
60
61 for(int i=0; i<str.length(); i++){ //数组名.length; 字符串对象.length()
62 System.out.println(str.charAt(i)); //public char charAt(int index):返回指定索引处的char值
63 }
64 }
65
66 public static void stringTest27(){
67 Scanner sc = new Scanner(System.in);
68 System.out.println("请输入一个字符串:");
69 String str = sc.nextLine();
70
71 int bigCount = 0;
72 int smallCount = 0;
73 int numberCount = 0;
74
75 for(int i=0; i<str.length(); i++){
76 char ch = str.charAt(i);
77 if(ch>='A' && ch<='Z'){
78 bigCount++;
79 }else if(ch>='a' && ch<='z'){
80 smallCount++;
81 }else if(ch>='0' && ch<='9'){
82 numberCount++;
83 }
84 }
85
86 System.out.println("大写字母个数:" + bigCount);
87 System.out.println("小写字母个数:" + smallCount);
88 System.out.println("数字个数:" + numberCount);
89 }
90
91 public static void stringTest28(){
92 int[] arr = {1, 2, 3, 4};
93 String str = arrToStr(arr);
94 System.out.println(str);
95 }
96
97 public static String arrToStr(int[] arr){
98 String str = "";
99 str+="[";
100 for(int i=0; i<arr.length; i++){
101 if(i == arr.length-1){
102 str+=arr[i];
103 }else {
104 str+=arr[i];
105 str+=", ";
106 }
107 }
108 str+="]";
109 return str;
110 }
111
112 public static void stringTest29(){
113 String str = "abcd";
114 String newStr = reverse(str);
115 System.out.println(newStr);
116 }
117
118 public static String reverse(String s){
119 String ss = "";
120 for(int i=s.length()-1; i>=0; i--){ //倒着遍历
121 ss+=s.charAt(i);
122 }
123 return ss;
124 }
125
126 }