一、String类
1、实例化方法
1)传统方法:
String str = new String("Hello");
2) 直接赋值:
String str = "Hello";
2、字符串相等比较(“==”和“equals”区别)
1)“==”字符串比较:
String str1 = "csdn";
String str2 = new String ("csdn");
System.out.println("str1 == str2");//"=="比较str1、str2堆内存的地址
2)equals进行字符串的比较:
若 str1 和 str2 中内容相同,则为true;否则为false。
3)"=="和equals的区别:
"=="表示进行数值比较,若比较字符串则比较的是字符串地址的数值
equals表示进行字符串内容的比较
3、String的匿名对象
String str = "Hello";
本质 : 将⼀个匿名的String类对象设置有名字,并且匿名对象保存在堆内存中。
4、实例化区别:
请解释String类中两种对象实例化的区别:
1)传统赋值(构造方法):开辟两块内存空间,其中一块为垃圾内存,不在对象池中保存,
但可手工入池(public String intern())。
2)直接赋值:只开辟一块堆内存空间,该字符串对象可自动保存在对象池中以供下次使用。
5、字符串不可变更:
字符串一旦定义后就不能进行修改。因为字符串是通过字符数组来实现的,数组定义好后,长度固定,所以不可修改。
String str = "Good";
str = str + "night";
str+=" !!!"
System.out.println(str);
观察上述代码,会发现打印了"Good night !!!",原因是因为str所指向的内存地址发生了变化。
6、字符与字符串
字符串就是一个字符数组,所以在String类里面支持有字符数组转换为字符串以及字符串变为字符的操作方法;
方法名称 | 类型 | 描述 | |
---|---|---|---|
1 | public String(char value[]) | 构造 | 将字符数组所有的内容变为字符串 |
2 | public String(char value[],int offset,int count) | 构造 | 将字符数组的部分内容变为字符串 |
3 | public char charAt(int index) | 普通 | 取得指定索引位置的字符,索引从零开始 |
4 | public char[] toCharArray() | 普通 | 将字符串变为字符数组返回 |
7、字节与字符串
字节常用于数据传输以及编码转换的处理之中,在String中提供对字节的支持。
方法名称 | 类型 | 描 述 | |
---|---|---|---|
1 | public String(byte bytes[]) | 构造 | 将字节数组变为字符串 |
2 | public String(byte byte[],int offset,int length) | 构造 | 将字节数组的部分内容变为字符串 |
3 | public byte[] getBytes() | 普通 | 将字符串以字符数组的形式返回 |
4 | public byte[] getBytes(String charsetName)throws UnsupportedEncodingException | 普通 | 编码转换处理 |
8、字符串比较
方法名称 | 类型 | 描述 | |
---|---|---|---|
1 | public boolean equals(Object anObject) | 普通 | 区分大小写的比较 |
2 | public boolean equalsIgnoreCase(String anotherString) | 普通 | 不区分大小写的比较 |
3 | public int compareTo(String annotherString) | 普通 | 比较两个字符串大小关系(能返回差值) |
不区分大小写的比较:
String str1 = "good" ;
String str2 = "Good" ;
System.out.println(str1.equals(str2)); // false
System.out.println(str1.equalsIgnoreCase(str2)); // true
compareTo()方法:比较两个字符串时,基于各个字符的Unicode值进行比较(相等返回0,小于返回负数,大于返回正数)
System.out.println("A".compareTo("a"));//-32
System.out.println("a".compareTo("A"));//32
System.out.println("A".compareTo("A"));//0
9、字符串查找
可以从一个完整的字符串中判断指定内容是否存在
方法名称 | 类型 | 描述 | |
---|---|---|---|
1 | public boolean contains(char sequence s) | 普通 | 判断一个字符串是否存在 |
2 | public int indexOf(String str) | 普通 | 从头开始查找指定字符串的位置查到了返回位置的开始索引,如果查不到返回-1 |
3 | public int indexOf(String str,int fromIndex) | 普通 | 从指定位置开始查找子字符串的位置 |
4 | public int lastIndexOf(String str) | 普通 | 由后向前查找子字符串的位置 |
5 | public int lastIndexOf(String str,int fromIndex) | 普通 | 从指定位置由后向前查找 |
6 | public boolean startsWith(String prefix) | 普通 | 判断是否以指定字符串开头 |
7 | public boolean startsWith(String prefix,int toffset) | 普通 | 从指定位置开始判断是否以指定字符串开头 |
8 | public boolean endsWith(String suffix) | 普通 | 判断是否以指定字符串结尾 |
10、字符串替换
使用一个指定的新的字符串替换原有字符串的数据
方法名称 | 类型 | 描述 | |
---|---|---|---|
1 | public String replaceAll(String regex,String replacement) | 普通 | 替换所有指定内容 |
2 | public String replaceFirst(String regex,String replacement) | 普通 | 替换首个内容 |
11、字符串拆分
方法名称 | 类型 | 描述 | |
---|---|---|---|
1 | public String [] split (String regex) | 普通 | 将字符串全部拆分 |
2 | public String [] split (String regex,int limit) | 普通 | 将字符串部分拆分,数组长度为limit极限值 |
12、字符串截取
方法名称 | 类型 | 描述 | |
---|---|---|---|
1 | public String substring(int beginindex) | 普通 | 从指定位置截取到结尾 |
2 | public String substring (int beginindex,int endindex) | 普通 | 截取部分内容 |
13、其他操作方法
方法名称 | 类型 | 描述 | |
---|---|---|---|
1 | public String trim() | 普通 | 去掉字符串的左右空格,保留中间空格 |
2 | public String toUpperCace() | 普通 | 字符串转大写 |
3 | public String toLowerCase() | 普通 | 字符串转小写 |
4 | public native String intern() | 普通 | 字符串入池操作 |
5 | public String concat(String str) | 普通 | 字符串连接,等于+,不入池 |
6 | public int length() | 普通 | 取得字符串长度 |
7 | public boolean isEmpty() | 普通 | 判断是否为空字符串,但不是null,是长度为0 |