-
String类的常用的判断功能:
-
boolean equals(Object obj):将此字符串与指定的对象比较
boolean equalsIgnoreCase(String str):将此字符串和另一个字符串进行比较,忽略大小写
boolean contains(String str):判断此字符串中是否包含str这个子字符串
boolean startsWith(String str):判断是否以str子字符串开头
boolean endsWith(String str):判断是否已str字符串结尾
boolean isEmpty():判断字符串是否为空*注意:
String s = “” ;表示s字符串是空字符串
String s = null ;空对象,连对象都没有.
public class StringDemo {
public static void main(String[] args) {
//定义三个字符串
String s1 = "helloworld" ;
String s2 = "helloworld" ;
String s3 = "HelloWorld" ;
// boolean equals(Object obj):将此字符串与指定的对象比较
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//false
System.out.println("-------------------------");
// boolean equalsIgnoreCase(String str):将此字符串和另一个字符串进行比较,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2)); //true
System.out.println(s1.equalsIgnoreCase(s3)); //true
System.out.println("-------------------------");
//boolean contains(String str):判断此字符串中是否包含str这个子字符串
System.out.println(s1.contains("h")); //true
System.out.println(s1.contains("hello")); //true
System.out.println(s1.contains("wor")); // true
System.out.println("-------------------------");
//boolean startsWith(String str):判断是否以str子字符串开头
System.out.println(s2.startsWith("wor")); //false
System.out.println(s2.startsWith("h")); //true
System.out.println("-------------------------");
//boolean endsWith(String str):判断是否已str字符串结尾
//boolean isEmpty():判断字符串是否为空
System.out.println(s1.isEmpty()); //false
String s4 = " " ;//空格
System.out.println(s4.isEmpty()); //false
System.out.println("-------------------------");
String s5 = null ;//NullPointerException:空指针异常
//加入非空判断
if(s5 !=null){
System.out.println(s5.isEmpty());
}
}
}