目录
心情感悟
知识点
string字符串
equals方法
length方法
面试题:
看提示:
charAt方法
判断指定字符串是否存在indexof方法
练习
字符串的截取subsatring方法
练习
一些基本的字符串方法
字符串的替换
字符串和数组的转换
字符串与其他数据类型的转换
包装器类型(包装类、封装类)
包装器方法
心情感悟
今天的新内容比较简单,就是记起来费大脑。旧内容就不一样了,难到起飞,哪个员工管理系统得优化到什么时候!!!吐了(๑ó﹏ò๑)。。。
知识点
string字符串
string是一个类
string类有哪些结构?
string一旦声明不可改变!!!
可以改变:基本变量赋值后,原来的值不存在了,叫可以改变
string被重新赋值后,原来的值存储在原地址(堆)中,string赋值后只是调用新地址的数据
public static void main(String[] args) {
//string 的声明:
//string既然是一个类,s1应该叫string类的对象
//类要创建对象要用new,s1并没有new,也可以叫对象,为什么?
String s1 = "abcdefg";
//s2是通过new创建的string类的对象
//创建对象要用构造器
String s2 = new String("abcdefg");
//以上两种写法不是一回事,
//第二种s2就是正常的调用构造器创建对象
//数据存储在堆里,地址在栈里,通过调用栈中地址找到真正数据
//第一种s1,数据不存储在堆里,new的东西才在堆里。常量(s1)存储在常量池(之前的叫法)中
//双等号比较的是虚地址
//虚地址:对象在内存中的存储位置(一般:堆中)
System.out.println( s1 == s2);
//只要出现了new就代表新建堆中地址。
String s3 = new String("abcdefg");
System.out.println(s2 == s3);
//
String s4 = ("abcdefg");
System.out.println( s1 == s4);
//=赋值怎么比较都是true
//new赋值,用双等号比较,就是false
}
equals方法
1、需要传参:需要传string类型的参数
2、有返回值:返回值为boolean类型
3、访问权限:public
string是一个类
string类有哪些结构?
string一旦声明不可改变!!!
可以改变:基本变量赋值后,原来的值不存在了,叫可以改变
string被重新赋值后,原来的值存储在原地址(堆)中,string赋值后只是调用新地址的数据
1、需要传参:需要传string类型的参数
2、有返回值:返回值为boolean类型
3、访问权限:public
public static void main(String[] args) {
String s1 = "a";
String s2 = "a";
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串1:");
String s3 = sc.next();
System.out.println("请输入字符串2:");
String s4 = sc.next();
//s3、s4都是对象,都可以调方法
//比较字符串内容,不比地址
System.out.println(s3.equals(s4));
}
length方法
1、不需要传参
2、有返回值,整型int
3、访问权限public
面试题:
字符串获取长度的方法和数组获取长度的方法有什么区别?
数组的length是属性,字符串的length()是方法
看提示:
在提示页面,第一个是方法名,括号里是参数类型,最后为返回值类型。
public static void main(String[] args) {
String s1 = "abcdefg";
//长度
//字符串:字符穿成的串
//,字符砖有下标(索引),从零开始
System.out.println(s1.length());
int []arr = new int[10];
System.out.println(arr.length);
}
charAt方法
1、不需要传参
2、有返回值,char类型
3、访问权限public
1、不需要传参
2、有返回值,整型int
3、访问权限public
面试题:
字符串获取长度的方法和数组获取长度的方法有什么区别?
数组的length是属性,字符串的length()是方法
看提示:
在提示页面,第一个是方法名,括号里是参数类型,最后为返回值类型。
public static void main(String[] args) {
String s1 = "abcdefg";
//长度
//字符串:字符穿成的串
//,字符砖有下标(索引),从零开始
System.out.println(s1.length());
int []arr = new int[10];
System.out.println(arr.length);
}
charAt方法
1、不需要传参
2、有返回值,char类型
3、访问权限public
在提示页面,第一个是方法名,括号里是参数类型,最后为返回值类型。
charAt方法
1、不需要传参
2、有返回值,char类型
3、访问权限public
public static void main(String[] args) {
String s1 = "abcdefg";
//取出指定下标位置的字符用charAt
//字符串下标越界:
// System.out.println(s1.charAt(100));
System.out.println(s1.charAt(0));
Scanner sc = new Scanner(System.in);
char c = sc.next().charAt(0);
System.out.println(c + 1);
}
判断指定字符串是否存在
indexof方法
1、需要传入参数
2、有返回值:返回值为字符串所在下标(int类型),
而且是从左到右遇到的第一个匹配的下标,如果要继续找,加第二个参数,如果不存在返回-1
indexof (str: , int: )代表从int位置开始查找,包括当前位置
1、需要传入参数
2、有返回值:返回值为字符串所在下标(int类型),
而且是从左到右遇到的第一个匹配的下标,如果要继续找,加第二个参数,如果不存在返回-1
indexof (str: , int: )代表从int位置开始查找,包括当前位置
public static void main(String[] args) {
String s1 = "abcdefgabcdaaa";
//判断指定字符是否存在
System.out.println(s1.indexOf("a",0));
//从后往前找
System.out.println(s1.lastIndexOf("a",12));
}
练习
统计字符串在另一个字符串中出现的次数
“abcdefgabcdaaa”中“a”出现了多少次?
法一
indexof方法:
public static void main(String[] args) {
String s1 = "abcdefgabcdaaa";
int index = 0;
for (int i = 0; i <= s1.length(); i++) {
if (s1.indexOf("a",i) == i){
index ++;
}
}
System.out.println(s1 + "中a出现了" + index + "次!");
}
法一另一种写法:
public static void main(String[] args) {
int i = 0;
int count = 0;
int target = 'a';
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 + "次!");
}
法二
charAt方法:
public static void main(String[] args) {
String s2 = "abcdefgabcdaaa";
int count1 = 0;
char target1 = 'a';
for (int j = 0; j < s2.length(); j++) {
if (s2.charAt(j) == target1){
count1 ++;
}
}
System.out.println(s1 + "中a出现了" + count1 + "次!");
}
法三
面向对象的方法,设计方法:
设计方法:
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"));
}
字符串的截取
subsatring方法
需要传参:传一个参数,就是从指定位置开始截取,直到字符串的末尾
传两个参数,就是从指定位置截取到指定位置
截取时包含起始截取位置的字符,不包含终止位置的字符。
统计字符串在另一个字符串中出现的次数
“abcdefgabcdaaa”中“a”出现了多少次?
法一
indexof方法:
法一另一种写法:
法二
charAt方法:
法三
面向对象的方法,设计方法:
字符串的截取
subsatring方法
需要传参:传一个参数,就是从指定位置开始截取,直到字符串的末尾
传两个参数,就是从指定位置截取到指定位置
截取时包含起始截取位置的字符,不包含终止位置的字符。
public static void main(String[] args) {
String str = "abcdefghijklmn";
//传一个参数,就是从指定位置开始截取,直到字符串的末尾
//考虑一下:包不包括指定位置的字符?
//包括
str.substring(1);
str.substring(1,2);
}
练习
键盘出入身份证号,输出性别(倒数第二位奇数为男,偶数为女),生日
2203221990090914567
法一:不用方法
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入身份证号:");
String id = sc.next();
if (id.length() ==18){
String birthday = id.substring(6,14);
String gender = "男";
//char 与 int 转换奇偶性不变
int genderNum = id.charAt(16);
if (genderNum % 2 == 0){
gender = "女";
}
System.out.println("身份证号为" + id + "生日为:" + birthday + "性别为:" + gender);
} else{
System.out.println("身份证号格式不正确!");
}
}
法二:设计方法
public String getGender(String id) {
String gender = "男";
int genderNum = id.charAt(16);
if (genderNum % 2 == 0) {
gender = "女";
}
return gender;
}
public String getBirthday(String id) {
return id.substring(6, 14);
}
主方法:
public static void main(String[] args) {
Ch06_Demo c = new Ch06_Demo();
Scanner sc = new Scanner(System.in);
System.out.println("请输入身份证号:");
String id = sc.next();
if (id.length() ==18){
System.out.println("身份证号为:" + id + ",生日:" + c.getBirthday(id) + ",性别:" + c.getGender(id));
}else{
System.out.println("输入有误,请重新输入!");
}
}
一些基本的字符串方法
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);
}
}
字符串的替换
字符串的替换
public static void main(String[] args) {
String str = "hello";
System.out.println(str.replace("e", "c"));
System.out.println(str.replaceAll("l", "i"));
}
字符串和数组的转换
public static void main(String[] args) {
字符串和数组的转换
数组转字符串,遍历数组,一个个转化
int []arr = new int[]{1,2,3,4,5};
字符串转字符型数组
String str = "abcdefg";
char []array = str.toCharArray();
for (char c : array){
System.out.println(c);
}
转乘字节型数组
一般情况下,字节型数组在操控文件时常用的IO流
byte [] bytes = str.getBytes();
for (byte b : bytes){
System.out.println(b);
}
字符串与其他数据类型的转换
int i = 10; 任何数据和字符串做加法,结果都是字符串 一般来说,其他类型转化字符串(阿里)不允许这么转:string s = i + "" String s = i + "1"; 一般转string用valueof() int 类型转换为string String s1 = String.valueOf(i); System.out.println(s); }
包装器类型(包装类、封装类)
八种数据类型对应八个类
1、byte---------Byte
2、short--------Short
3、int----------Integer
4、long---------Long
5、float--------Float
6、double-------Double
7、char---------Character
8、boolean------Boolean
为什么要用包装器类型?
今天起淘汰基本数据类型
淘汰场景:默认值不同,基本数据类型默认值为0,包装器类型默认值为null。
原理
装箱:把Integer包装成int类型。
基本数据类型转换成对应的包装器类型
拆箱:把int包装成Integer类型。
包装器类型转换成对应的基本数据类型
JDK5之后的新功能:
1.自动装箱、拆箱
2.增强for循环(foreach)
3.枚举
JDK7之后的新功能:
1.switch...case 可以用String
八种数据类型对应八个类
1、byte---------Byte
2、short--------Short
3、int----------Integer
4、long---------Long
5、float--------Float
6、double-------Double
7、char---------Character
8、boolean------Boolean
为什么要用包装器类型?
今天起淘汰基本数据类型
淘汰场景:默认值不同,基本数据类型默认值为0,包装器类型默认值为null。
原理
装箱:把Integer包装成int类型。
基本数据类型转换成对应的包装器类型
拆箱:把int包装成Integer类型。
包装器类型转换成对应的基本数据类型
JDK5之后的新功能:
1.自动装箱、拆箱
2.增强for循环(foreach)
3.枚举
JDK7之后的新功能:
1.switch...case 可以用String
public static void main(String[] args) {
Integer i = 0;
Integer i2 = new Integer(30);//过时了!!!
}
包装器方法
1.数组下标越界
2.内存溢出
3.空指针
4.字符串下标越界
5.数字格式化(字符串里不是数字,转换成int型时异常)
public static void main(String[] args) {
//字符串转化整数
String s = "1990";
//一般转成int.但可以自动装箱
Integer i = Integer.parseInt(s);
//转回去
Integer.toString(i);
System.out.println(2022 - 1990);
}
1.数组下标越界
2.内存溢出
3.空指针
4.字符串下标越界
5.数字格式化(字符串里不是数字,转换成int型时异常)



