目录
String类的判断功能:boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
boolean startsWith(String str)
点击下方链接学习更多的String类 API深入研究
字符串String API深入研究1.0
String类的获取功能 API深入研究3.0
String类的字符串转换功能 API深入研究4.0
String类的替换功能、去除字符串两端空格、按字典顺序比较两个字符串 API深入研究5.0
在我们企业的日常开发中,String类是每天都会去使用的高频类,那么我们来深入探索一下。(个人参考API文档编写,仅供学习参考)
字符串:简单理解,就是由多个字符组成的数据,叫做字符串;也可以看作是一个字符数组。
观察API发现:
1、String代表的是字符串,属于java.lang下面的。所以使用的时候不需要导包。
2、String类代表字符串,Java程序中的所有字符串文字(例如"abc"),都被实现为此类的实例(对象)。
3、字符串不变:它们的值在创建后不能被更改,字符串是常量,一旦被赋值,就不能改变。
注意事项:String类重写了toString()方法。
String类的判断功能:
boolean equals(Object obj)
查看API文档我们知道:
将此字符串与指定对象进行比较。 其结果是true
当且仅当该参数不是null
并且是String
对象,表示相同的字符序列作为该对象。
重写:
参数
anObject
- 对比这个 String
的对象
结果
true
如果给定的对象代表一个 String
等效于这个字符串,否则为false
顾名思义就是比较字符串的内容是否相等,区分大小写
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
String s4 = "helloworld";
//boolean equals(Object obj) 比较字符串的内容是否相同 区分大小写
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equals(s4));
}
}
false
false
trueProcess finished with exit code 0
boolean equalsIgnoreCase(String str)
查看API文档我们知道:
将此String
与其他String
比较,忽略案例注意事项。 如果两个字符串的长度相同,并且两个字符串中的相应字符等于忽略大小写,则两个字符串被认为是相等的。
如果以下至少一个为真,则两个字符c1
和c2
被认为是相同的忽略情况:
- 两个字符相同(与
==
操作符相比) - 将方法Character.toUpperCase(char)应用于每个字符产生相同的结果
- 将方法Character.toLowerCase(char)应用于每个字符产生相同的结果
参数
anotherString
- String
将此 String
对比
结果
true
如果参数不是null
,它代表等效的String
忽略大小写; 否则为false
顾名思义就是比较字符串的内容是否相同,忽略大小写
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
String s4 = "helloworld";
//boolean equalsIgnoreCase(String str)
//比较字符串的内容是否相同,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s1.equalsIgnoreCase(s3));
System.out.println(s1.equalsIgnoreCase(s4));
}
}
true
true
trueProcess finished with exit code 0
boolean contains(String str)
查看AIP文档我们知道:
当且仅当此字符串包含指定的char值序列时才返回true。
参数
s
- 搜索的顺序
结果
如果此字符串包含 s
,则为true,否则为false
顾名思义就是当且仅当此字符串包含指定的char值序列时才返回true;判断大的字符串中是否包含小的字符串,如果包含,返回true,如果不包含,返回false,区分大小写。
参考代码:
/*
当且仅当此字符串包含指定的char值序列时才返回true。
判断大的字符串中是否包含小的字符串,如果包含,返回true,反之false
区分大小写
*/
public class testStringDemo1 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
String s4 = "helloworld";
//boolean contains(String str)
System.out.println(s1.contains("Hello"));
System.out.println(s1.contains("leo"));
System.out.println(s1.contains("hello"));
}
}
false
false
trueProcess finished with exit code 0
boolean startsWith(String str)
查看AIP文档我们知道:
测试此字符串是否以指定的前缀开头。
参数
prefix
- 前缀。
结果
true
如果由参数表示的字符序列是由该字符串表示的字符序列的前缀; false
否则。 还需要注意的是true
如果参数为空字符串或等于该将被返回String
如由所确定的对象equals(Object)方法。
顾名思义就是测试字符串是否以指定的前缀开头,区分大小写。
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
String s4 = "helloworld";
//boolean startsWith(String str)
//测试此字符串是否以指定的前缀开头,区分大小写
System.out.println(s1.startsWith("hel"));
System.out.println(s1.startsWith("h"));
System.out.println(s1.startsWith("he"));
System.out.println(s1.startsWith("he34"));
System.out.println(s1.startsWith("H"));
}
}
true
true
true
false
falseProcess finished with exit code 0
boolean endsWith(String str)
查看AIP文档我们知道:
测试此字符串是否以指定的后缀结尾。
参数
suffix
- 后缀。
结果
true
如果由参数表示的字符序列是由该对象表示的字符序列的后缀; false
否则。 注意,结果将是true
如果参数是空字符串或等于该String
如由所确定的对象equals(Object)方法。
顾名思义就是测试此字符串是否以指定的后缀结束,区分大小写。
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
String s4 = "helloworld";
//boolean endsWith(String str)
//测试此字符串是否以指定的后缀结束,区分大小写
System.out.println(s1.endsWith("orld"));
System.out.println(s1.endsWith("orlD"));
}
}
true
falseProcess finished with exit code 0
boolean isEmpty()
查看AIP文档我们知道:
退货 true
如果,只有 length()是 0
。
结果
true
如果 length()是 0
,否则 false
顾名思义就是判断字符串是否是空字符串。
参考代码:
public class testStringDemo1 {
public static void main(String[] args) {
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
//boolean isEmpty() 判断字符串是否是空字符串
System.out.println(s1.isEmpty());
System.out.println("**************************");
String s4 = "";
String s5 = null;
System.out.println(s4==s5);
System.out.println(s4.isEmpty());
//NullPointerException
// System.out.println(s5.isEmpty());
}
}
false
**************************
false
trueProcess finished with exit code 0
注意:boolean isEmpty()是判断是否为空字符串,如果字符串为空则返回true,反之false,千万不要搞混淆了!
String s6 = null;这样定义字符串是错的!否则报错NullPointerException空指针异常错误。
字符串之间比较的要求,在不知道两个字符串变量的值的时候,为了防止 空指针异常,把变量放在后面。
参考代码:
/**
* 字符串之间比较的要求,在不知道两个字符串变量的值的时候,为了防止
* 空指针异常,把变量放在后面
*/
public class testStringDemo1 {
public static void main(String[] args) {
String s6 = "bigdata";
String s7 = null;
// System.out.println(s6.equals(s7));
// System.out.println(s7.equals(s6));
//需求:将s6,s7与"hadoop"进行比较
// System.out.println(s6.equals("hadoop"));
// System.out.println(s7.equals("hadoop"));
// 推荐写法
System.out.println("hadoop".equals(s6));
System.out.println("hadoop".equals(s7));
System.out.println("bigdata".equals(s6));
}
}
false
false
trueProcess finished with exit code 0
注意:在今后的字符串比较我们要把要比较的字符串写在前面,变量写在后。
到底啦!您收获到了什么呢?给靓仔点个关注吧!