2022-07-22第七组姜珊
String
string字符串,是一个类(有长度)
String s1="abcdef";
String s3="abcdef";
String s2=new String(abcdef);
s1!=s2 地址不同
adcdef 赋值给s1 s3 常量池只有一个adcdef
==比较的是堆地址
new代表新建;
String一旦声明不可改变:地址还在;
=和equaurs
==地址比较 (栈内存)
equaurs 和比较(堆内存)
字符串比较
1.比较内容
equals方法:
- 需传参,传Sting参数。
- 有返回值,boolean类型
- 访问权限-public
length方法:
- 不需要传参
- 有返回值-整型类型
- 访问权限:public
- 数组length是属性,字符串的length()是方法
下标(索引):
String s1="asdfgh";
s1.charAt(0)
system.out,println(s1.charAt(0));
Scanner sc=new Scanner(System,in);
char c=sc.next.charAt(0)
s1.charAt(0)取出下标位置字符
判断指定字符串是否存在:
返回值为字符串在s1的下标,返回从左到右第一个匹配的标
String s1="asdfgh";
system.out,println(s1.indext("a",0));
indexof(String,int)从int位置开始找,包括当前位置
s1.lastIndex of从后往前找
字符串的截取
String str="qwerty";
str,substing(1);
str,substring(1,2)
包含起始不包含终止
如果传一个参数,从指定位置开始截取,直到字符串的末尾包括起始位置字符,生成新的,不改变原有数据
如果传两个参数,从指定位置开始截取,直到下一个参数指定位置结束,包含起始不包含终止,生成新的,不改变原有数据
判断是xxx开头
system.out,println(str.startswith("a",2));
判断是xxx结尾
system.out,println(str.endswith("a",2));
转大写
system.out,println(str.toUpperCase);
转小写
system.out,println(str.to lowerCase);
忽略大小写比较
str.equals Ignore case(str1)
去掉字符串前后空格
Str1,trim()
根据指定字符分割
String[]string s=str 2.split(",")
根据,分割,分割后的数保存到数组
字符串的替换
string str="hello";
Str.replace (old char,new char)
str.replace All(字符串)
字符串和数组转换,和其他数据类型转换
任何数据类型和字符串类型做加法,效果都是字符串
String s=string value of()
数组转字符串
int[]arr=new int[]{1.2.3.4.5}
字符串转数组
String str="asdfgh"
char[]array=str.tocharArray()
转成字节型数组(二进制)
byte[]bytes=str.get Bytes;
八大类包装器类型
byte Byte
short:Short
int:Integer
long Long
float Float
double Double
char Character
boolean Boolean
装箱:
基本数据类型转换成对应包装器类型
拆箱:
包装器类型类型转换成对应基本数据
JDK5新功能:
- 1.自动装箱和自动拆箱
- 增强For循环
- 枚举
JDK7新功能:
Switch...case可以用String
异常:
- 数组下标越界
- 内存溢出
- 字符串下标越界
- 数字格式化
实例
Demo类
Employee类
EmployeeManager类
User类
UserManager类
心得体会:
每行代码理解,但是自己写起来费劲,封装思路不清晰