- equals
请解释String类中两种对象实例化的区别- 直接赋值:只会开辟一块堆内存空间,并且该字符串对象可以自动保存在对象池中以供下次使用。
string str = “hello”; - 构造方法:会开辟两块堆内存空间,其中一块成为垃圾空间,不会自动保存在对象池中,可以使用intern()方法手工入池。
string str = new string(“hello”)
- 直接赋值:只会开辟一块堆内存空间,并且该字符串对象可以自动保存在对象池中以供下次使用。
int a = 10;
int c = 10;
String str1 = new String("hello");
String str2 = new String("hello");
String str3 = "cat";
String str4 = "cat";
System.out.println(a == c);//true
System.out.println(str1 == str2);//false == 在Java中比较的是地址
System.out.println(str3 == str4);//true
System.out.println("hello".equals(str1)); //equals比较的是字符串
public class test {
public static void main(String[] args) {
String str1 = new String("hello");//不在字符串常量池
String str2 = "hello";
System.out.println(str1==str2);//false
String str3 = new String("hello").intern();//手动加入常量池
String str4 = "hello";
System.out.println(str3==str4);//true
}
2 .compareTo
-
在String类中compareTo()方法是一个非常重要的方法,该方法返回一个整型,该数据会根据大小关系返回三类内容:
-
相等返回0;
-
小于返回小于0的数
-
大于返回大于0的数
System.out.println("A".compareTo("a")); // -32
System.out.println("a".compareTo("A")); // 32
System.out.println("A".compareTo("A")); // 0
System.out.println("AB".compareTo("AC")); // -1
System.out.println("刘".compareTo("杨"));//-5456
- toCharArray
String str = "helloworld" ;
// 将字符串变为字符数组
char[] data = str.toCharArray() ;//toCharArray转换为字符数组
for (int i = 0; i < data.length; i++) {
System.out.print(data[i]+" ");
}
// 字符数组转为字符串
System.out.println(new String(data)); // 全部转换 hellowworld
System.out.println(new String(data,5,5)); // 部分转换 hello
- contains
可用于字符串查找,查看子字符串是否在其中
String str = "hello";
String str2 = "helloworld" ;
System.out.println(str2.contains("world")); // true
System.out.println(str.contains("world")); // false
- indexOf
现在基本都是用contains()方法完成。
使用indexOf()需要注意的是,如果内容重复,它只能返回查找的第一个位置
String str = "helloworld" ;
System.out.println(str.indexOf("world")); // 5,w开始的索引
System.out.println(str.indexOf("bit")); // -1,没有查到
if (str.indexOf("hello") != -1) {
System.out.println("可以查到指定字符串!");
}
- lastIndexOf
查询返回的是下标
String str = "helloworld" ;
System.out.println(str.indexOf("l")); // 2
System.out.println(str.indexOf("l",5)); // 8 从指定下标开始查询
System.out.println(str.lastIndexOf("l")); // 8 从后开始查询
- replaceAll 8. replaceFirst
注意事项: 由于字符串是不可变对象 替换不修改当前字符串 而是重新创建一个新的字符串
String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_")); replaceAll 全都替换 he__owor_d
System.out.println(str.replaceFirst("l", "_"));replaceFirst 替换首个 he_loworld
- split
字符串的拆分
String str = "hello world hello bit" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {//循环
System.out.println(s);
}
//结果
hello
world
hello
bit
注意事项:
- 字符"|","*","+“都得加上转义字符,前面加上”".
- 而如果是"",那么就得写成"\".
- 如果一个字符串中有多个分隔符,可以用"|"作为连字符.
String str = "192.168.1.1" ;
String[] result = str.split("\\.") ;
for(String s: result) {
System.out.println(s);
}
多次拆分
String str = "name=zhangsan&age=18" ;
String[] result = str.split("&") ;
for (int i = 0; i < result.length; i++) {
String[] temp = result[i].split("=") ;
System.out.println(temp[0]+" = "+temp[1]);
}
//运行结果
name = zhangsan
age = 18
- subString
从一个完整的字符串之中截取出部分内容。
注意:- 索引从0开始
- 注意前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标
String str = "helloworld" ;
System.out.println(str.substring(5));
System.out.println(str.substring(0, 5));
//运行结果
world
hello
- trim
去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等).
String str = " hello world " ;
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");
//trim 会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等).
//运行结果
[ hello world ]
[hello world]
- isEmpty
判断字符串是否为空
System.out.println("hello".isEmpty());
System.out.println("".isEmpty());
System.out.println(new String().isEmpty());
- length
求字符串的长度
String str = " hello%$$%@#$%world 哈哈哈 " ;
System.out.println(str.length());