第2节String类
String概述
String类在java.lang包下,所以使用的时候不需要导包
String类代表字符串,Java程序中的所有字符串文字(例如“abc”)都被实现为此类的实例
也就是说,Java程序中所有的双引号字符串,都是String类的对象
字符串的特点
- 字符串不可变,它们的值在创建后不能被更改
- 虽然String的值是不可变的,但是它们可以被共享
- 字符串效果上相当于字符数组(char[ ]),但是底层原理是字节数组(byte[ ])
String构造方法
public String()//创建一个空白字符串对象,不含有任何内容 public String(char[] chs)//根据字符数组的内容,来创建字符串对象 public String(byte[] bys)//根据字符数组的内容,来创建字符串对象 String s="abc";//直接赋值的方式创建字符串对象,内容就是abc
package com.itheima_01; public class StringDemo { public static void main(String[] args) { //推荐使用直接赋值的方式得到abc //1 String s1=new String(); System.out.println("s1:"+s1); //2 char[] chs={'a','b','c'}; String s2=new String(chs); System.out.println("s2:"+s2); //3 byte[] bys={97,98,99}; String s3=new String(bys); System.out.println("s3:"+s3);//输出结果 s3:abc //4 String s4="abc"; System.out.println("s4:"+s4); } }
String对象的特点
1、通过new创建的字符串对象,每一次new都会申请一个内存空间,虽然内容相同,但是
地址值不同
char[] chs={'a','b','c'}; String s1=new String(chs); String s2=new String(chs);
什么的代码中,JVM会首先创建一个字符数组,然后每一次new的时候都会有一个新的地址,
只不过s1和s2参考的字符串内容是相同的
2、以""方式给出的字符串,只要字符序列相同(顺序和大小写),无论在程序代码中出现几次,
JVM都只会建立一个String对象,并在字符串池中维护
String s3="abc";
String s4="abc";
在上面的代码中,针对第一行代码,JVM会建立一个String对象放在字符串池中,并给s3参考;
第二行则让s4直接参考字符串池中的String对象,也就是说它们本质上是同一个对象
package com.itheima_01; public class StringDemo02 { public static void main(String[] args) { char[] chs={'a','b','c'}; String s1=new String(chs); String s2=new String(chs); System.out.println(s1==s2);//输出false String s3="abc"; String s4="abc"; System.out.println(s3==s4);//输出true System.out.println(s1==s3);//输出false //熟悉栈内存和堆内存 } }
字符串的比较
小结:使用==做比较
- 基本类型:比较的是数据值是否相同
- 引用类型:比较的是地址值是否相同
字符串是对象,它比较内容是否相同,是通过一个方法来实现的,这个方法叫:equals()
- public boolean equals(Object anObject):将此字符串指定对象进行比较。由于我们比较的是字符串对象,所以参数直接传递一个字符串
package com.itheima_01; public class StringDemo03 { public static void main(String[] args) { //构造方法的方式得到对象 char[] chs={'a','b','c'}; String s1=new String(chs); String s2=new String(chs); //直接赋值的方式得到对象 String s3="abc"; String s4="abc"; //比较字符串对象地址是否相同 System.out.println(s1==s2);//false System.out.println(s1==s3);//false System.out.println(s3==s4);//true //比较字符串内容是否相同 System.out.println(s1.equals(s2));//true System.out.println(s1.equals(s3));//true System.out.println(s3.equals(s4));//true } }
案例:用户登录
需求:已知用户名和密码,请用程序实现模拟用户登录。总共给三次机会,登录之后,给出相应的提示
package com.itheima_01; import java.util.Scanner; public class StringDemo04 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a=1; while(true){ //用户名admin 密码123 System.out.println("请输入用户名:"); String user=sc.nextLine(); System.out.println("请输入密码:"); String pwd=sc.nextLine(); if(user.equals("admin")&&pwd.equals("123")){ System.out.println("登录成功"); System.exit(0);//或者break }else{ System.out.println("用户名或密码错误,请重新输入:"); } a++; if(a>=4){ System.out.println("你输入的次数超过三次,密码已锁"); System.exit(0);//或者break } } } }
显示剩余几次机会
int b=3; ...... b--; System.out.println("用户名或密码错误,你还有"+b+"次机会");
案例:遍历字符串
需求:键盘录入一个字符串,使用 程序实现控制台遍历该字符串
思路:遍历字符串,首先要能够获取到字符串中的每一个字符
public char charAt(int index):返回指定索引处的char值,字符串的索引也是从0开始的
数组的长度:数组名.length
字符串长度:字符串对象.length()
package com.itheima_01; import java.util.Scanner; public class StringDemo05 { public static void main(String[] args) { //需求:键盘录入一个字符串,使用 程序实现控制台遍历该字符串 Scanner sc = new Scanner(System.in); System.out.println("请随便输入一串字符"); String _string = sc.nextLine(); for(int i=0;i<_string.length();i++){ System.out.println(_string.charAt(i)); } } }
案例:统计字符次数
需求:键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数(不考虑其它字符)
思路:判断该字符属于哪种类型,然后对应类型的统计变量+1
假如ch是一个字符,我要判断它属于大写字母,小写字母,还是数字,直接判断该字符是否在对应的范围即可
大写字母:ch>='A'&&ch<='Z'
....
package com.itheima_01; import java.util.Scanner; public class StringDemo06 { public static void main(String[] args) { // 需求:键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数(不考虑其它字符) Scanner sc=new Scanner(System.in); int max=0; int min=0; int num=0; int other=0; System.out.println("请输入一串字符包含大小写和数字:"); String line=sc.nextLine(); for(int i=0;i<line.length();i++){ //System.out.println(line.charAt(i)); if(line.charAt(i)>='A'&&line.charAt(i)<='Z'){ max++; }else if(line.charAt(i)>='a'&&line.charAt(i)<='z'){ min++; }else if(line.charAt(i)>='0'&&line.charAt(i)<='9'){ num++; }else{ other++; } } System.out.println("大写字母:"+max); System.out.println("小写字母:"+min); System.out.println("数字:"+num); System.out.println("其它字符:"+other); } }
案例:拼接字符串
需求:定义一个方法,把int数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,并在
控制台输出结果。例如,数组为int[] arr={1,2,3};,执行方法后的输出结果为:[1,2,3]
package com.itheima_01; /* * 需求:定义一个方法,把int数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,并在 * 控制台输出结果。例如,数组为int[] arr={1,2,3};,执行方法后的输出结果为:[1,2,3] * */ public class StringDemo07 { public static void main(String[] args) { int[] arr = {0,1,2}; StringDemo07 s = new StringDemo07(); System.out.println(s.show(arr)); } public String show(int[] arr) { String line = ""; for (int i = 0; i < arr.length; i++) { line += arr[i]; if (i < arr.length - 1) { line += ","; } } String line2 = ""; line2 = "[" + line + "]"; return line2; } }
package com.itheima_01; /* * 需求:定义一个方法,把int数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,并在 * 控制台输出结果。例如,数组为int[] arr={1,2,3};,执行方法后的输出结果为:[1,2,3] * */ public class StringDemo07 { public static void main(String[] args) { int[] arr = {0,1,2}; //StringDemo07 s = new StringDemo07(); System.out.println(show(arr)); } public static String show(int[] arr) { String line = ""; for (int i = 0; i < arr.length; i++) { line += arr[i]; if (i < arr.length - 1) { line += ","; } } String line2 = ""; line2 = "[" + line + "]"; return line2; } }
案例:字符串反转
需求:定义一个方法,实现字符串反转。键盘录入一个字符串,调用该方法后,在控制台输出结果
例如:键盘录入cba,输出结果abc;
package com.itheima_01; /* * 需求:定义一个方法,实现字符串反转。键盘录入一个字符串,调用该方法后,在控制台输出结果 * 例如:键盘录入cba,输出结果abc; * */ import java.util.Scanner; public class StringDemo08 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入一串字符"); String line=sc.nextLine(); System.out.println(lineResults(line)); } public static String lineResults(String line){ String results=""; for(int i=line.length()-1;i>=0;i--){ results+=line.charAt(i); } return results; } }
通过帮助文档查看String中的方法
public boolean equals(Object anObject) 比较字符串的内容,严格区分大小写(用户名和密码)
public char charAt(int index) 返回指定索引处的char值
public int length() 返回此字符串的长度

浙公网安备 33010602011771号