今日重点:
目录
一、String字符串
1、String概述
String字符串,String是一个类,中很多特性与数组一致
① 字符串一旦创建不可更改(String一旦声明不可改变,String一旦创建不可更改,使用时不要频繁拼接字符串,效率低,浪费空间,垃圾回收也存在问题。
②为了提升字符串的访问效率,Java提出字符串常量池,相当于一个缓冲区。存在于方法区中引用类型对象保存在堆内存,字符串保存在静态区的字符串常量池中
③在程序执行过程中,如果用到某个字符串,如“abc”,虚拟机会先去常量池中搜索,如果存在,直接指向该字符串,否则新建一个字符串对象,并指向。
1.1创建字符串
(1)用构造方法创建
String s=new String("good");
(2)用字符常量创建
String s1="good"; String s2="good";
此时s1和s2引用相同的字符串常量,也具有相同的实体,都存在常量池中 。
1.2输出字符串
字符串本身就是字符串,返回本身。
System.out.println("abc".toString()); //输出:abc
package com.jr.morning;
public class Ch01 {
public static void main(String[] args) {
//String 既然是一个类,s1应该叫做String类的对象
//如果一个类想要创建对象--new,new代表新建。s1没有new,s1也可以叫做对象
String s1="abcdefg";
//s2是通过new创建出来的String类对象
//创建对象是要调用构造器
String s2=new String("abcdefg");
String s3=new String("abcdefg");
String s4=("abcdefg");
//虽然s1和s2都是String类对象,但从内存角度说,s1和s2不是一回事
/*
两者指向的abcdefg不在同一个区域内,存储的地址不一样
*/
// 双等号比较的是虚地址。虚地址:对象在内存中的存储位置
System.out.println(s1 == s2);//结果为false
System.out.println(s2 == s3);//结果为false
System.out.println(s1 == s4);//结果为true
//总结:=赋值,无论怎么比较,都是true。new赋值,用双等号比较,就是false
}
}
通过new创建字符串对象,对象地址保存在堆内存中,每使用new一次,就在堆中开辟一块空间,字符串日常使用时:不使用new创建。s1和s2不在一个内存里,s1存在常量池(方法区)里,s2存在推里。
2、equal方法
public boolean equals(Object anObject)
equal方法;用于两个字符串之间的比较
1.需要传参,传String类型的参数
2.有返回值,类型是Boolean类型
3.访问权限是pubic注意:
操作符 == 来比较两个字符串值能检测s1和s2是否指向同一个对象,不会告诉你内容是否相等。因此不能用操作符 == 来比较字符串之间的内容。== 的作用:
基本类型:比较值是否相等
引用类型:比较内存地址值是否相等equals 的作用:
引用类型:默认情况下,比较内存地址值是否相等。可以按照需求逻辑,重写对象的equals方法。
package com.jr.morning;
import java.util.Scanner;
//String的常用方法
//对象都可以调方法
public class Ch02 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1="a";
String s2="a";
System.out.println("请输入字符串1:");
String s3=sc.next();
System.out.println("请输入字符串2:");
String s4=sc.next();
System.out.println(s3.equals(s4));
}
}
3、length方法
length方法:public int length()-----返回当前字符串的长度。
1.不需要传参
2.有返回值,类型是整型int
3.访问权限public
注意:访问数组的长度是length属性,访问字符串的长度是length()方法。(这是一个面试题,问: 字符串获取长度的方法和数组获取长度有什么区别?
数组的length是属性,字符串的length()是方法)
package com.jr.morning;
public class Ch03 {
public static void main(String[] args) {
String s1="abcdefg";
s1.length();
System.out.println(s1.length());
int[]arr=new int[10];
System.out.println(arr.length);
}
}
4、charActer方法
public char charAt(int index)
用于查找字符串中下标为index的字符,返回一个字符。如果要将字符串转成char类型,用以下方法:
char c=sc.next().charAt(0);//字符串转成char类型 System.out.println(c);//会得到char类型的返回值
拓展:
public char[] toCharArray()
将字符串转换成char[]数组,并返回。
测试代码:char[] chars = "student".toCharArray(); for (char c : chars) { System.out.print(c + " "); }
代码例子:
package com.jr.morning;
import java.util.Scanner;
public class Ch04 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1="abcdefg";
//取出指定下标位置的字符
System.out.println(s1.charAt(0));//结果会输出a,a的下标为0
//要注意下标越界(这最大大的下标为6,不可超过)
}
}
几种下标越界:
1、数组下标越界
2、内存溢出
3、空指针
4、字符串下标越界
5、indexOf方法
public int indexOf(String str)
返回某个子字符串在当前字符串中第一次出现的下标,没有就返回-1。String s1= "abcdefgabcdaaa"; System.out.println(s1.indexOf("a",8));
public int lastIndexOf(String str)
返回某个子字符串在当前字符串中最后一次出现的下标,没有就返回-1。String s1= "abcdefgabcdaaa"; System.out.println(s1.lastIndexOf("a"));
indexOf方法的练习:
需求: 统计一个字符串在另一个字符串中出现的次数
方法1:用for循环
package com.jr.morning; import java.util.Scanner; public class Ch05_Demo { public static void main(String[] args) { //求a在s1中出现的次数 Scanner sc = new Scanner(System.in); String s1= "abcdefgabcdaaa"; char target ='a'; int count=0; for (int i = 0; i < s1.length(); i++) { if (s1.charAt(i)==target){ count++; } } System.out.println(target+"在"+s1+"中出现了"+count+"次"); } }
方法2:用indexOf方法
package com.jr.morning; import java.util.Scanner; public class Ch05_Demo { public static void main(String[] args) { //求b在s1中出现的次数 Scanner sc = new Scanner(System.in); String s1= "abcdefgabcdaaa"; // 第二种 char target ='b'; int count=0; int i=0; while (i<s1.length()){ int i1=s1.indexOf(target,i); if (i1!=-1){ count++; i=i1+1; }else { i++; } } System.out.println(target+"在"+s1+"中出现了"+count+"次"); } }
方法3:封装成一个方法,以便重复使用(使用到面向对象的知识)
package com.jr.morning; public class Ch05_Demo { public int sum(String target, String str) { int count = 0; int i = 0; while (i < str.length()) { int i1 = str.indexOf(target, i); if (i1 != -1) { count++; i = i1 + 1; } else { i++; } } return count; } public static void main (String[]args){ Ch05_Demo c =new Ch05_Demo(); System.out.println(c.sum("b","abcdefgabcdaaa")); } }
6、substring方法
public String substring(int beginIndex, int endIndex)
在当前字符串中,从beginIndex开始截取,截取到endIndex的新字符串,返回新字符串。
注意:beginIndex是包括的,endIndex是不包括的。
左闭右开:[beginIndex, endIndex) 或 [beginIndex, endIndex-1]。String str1 = "abcdefgh".substring(3, 6); System.out.println(str1); //输出:def
package com.jr.morning; public class Ch06 { public static void main(String[] args) { //字符串的截取--substring String str="abcdefgijklm"; /* 如果传一个参数,从指定位置开始截取,直到字符串的末尾 包括起始位置的字符,会生成一个新的字符串,不会改变原有的数据 */ String s= str.substring(1);//输出的结果为bcdefgijklm s=str.substring(1,2);//从1截到2,输出的结果为b //bc or b //包含起始位置,不包含终止位置 System.out.println(s); } }
substring方法练习:
要求:键盘上输入身份证号,判断输出性别,生日
代码1:
package com.jr.morning; import java.util.Scanner; /* 键盘上输入身份证号,判断输出 性别,生日 */ public class Ch06_Dome { public static void main(String[] args) { Scanner sc=new Scanner(System.in); /* 1.倒数第2个数(第17个数),然后判断是是偶数还是奇数,偶女奇男 2.第7~14位数字代表生日 */ // 判断此身份证号: 220122199009094567 System.out.println("请输入身份证号:"); String cardId=sc.next(); if (cardId.length()==18){//字符的长度定为18 String birthday=cardId.substring(6,14); String gender ="男"; char genderNum=cardId.charAt(16); if (genderNum %2==0){ gender="女"; } System.out.println("身份证号为:"+cardId+","+"生日为:"+birthday+","+"性别为:"+gender); }else { System.out.println("输入有误,请重新输入!"); } } }
在此基础上设计方法:
package com.jr.morning; import java.util.Scanner; /* 键盘上输入身份证号,判断输出 性别,生日 */ public class Ch06_Dome { public String getGender(String cardId){//获取性别 String gender ="男"; char genderNum=cardId.charAt(16); if (genderNum %2==0){ gender="女"; } return gender; } public String getBirthday(String cardId){//获取生日 return cardId.substring(6,14); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); Ch06_Dome c=new Ch06_Dome();//调用函数 /* 1.倒数第2个数(第17个数),然后判断是是偶数还是奇数,偶女奇男 2.第7~14位数字代表生日 */ // 判断此身份证号: 220122199009094567 System.out.println("请输入身份证号:"); String cardId=sc.next(); if (cardId.length()==18){//字符的长度定为18 String birthday=c.getBirthday(cardId); String gender=c.getGender(cardId); //如果不想写上面这两行代码,可以直接只写下面这一行代码 // System.out.println("身份证号为:"+cardId+","+"生日为:"+c.getBirthday(cardId)+","+"性别为:"+c.getGender(cardId)); System.out.println("身份证号为:"+cardId+","+"生日为:"+birthday+","+"性别为:"+gender); }else { System.out.println("输入有误,请重新输入!"); } } }
7、一些不常用的String方法
public static void main(String[] args) {
String str = "abcdEFG";
String str1 = " ABCDEFG ";
String str2 = "123,456,789,00";
/*
下面所有的方法都是返回一个新的字符串
*/
// 转大写
System.out.println(str.toUpperCase());
// 转小写
System.out.println(str.toLowerCase());
// 判断是否以xxxx开头
System.out.println(str.startsWith("a",2));
// 判断是否以xxxx结尾
System.out.println(str.endsWith("F"));
// 忽略大小写进行比较内容
// 验证码
System.out.println(str.equalsIgnoreCase(str1));
// 去掉字符串前后的空格
System.out.println(str1.trim());
// 根据指定的字符分割,分割之后,分割条件是消失
String [] strings = str2.split(",");
// System.out.println(Arrays.toString(strings));
for (String string : strings) {
System.out.println(string);
}
}