package cn.lgt.stringmethods;
public class StringJudgeMethods {
public static void judgeMethods(){
//String类的判断功能(方法)
//1. 比较两个字符串的内容是否相同,区分大小写
//boolean equals(Object obj);
String s1 = "hello";
String s2 = "world";
String s3 = "hello";
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s2.equals(s3));
System.out.println(s1.equals(s1));
//2. 比较两个字符串的内容是否相同,忽略大小写
//boolean equalsIgnoreCase(Object obj);
String s4 = "HELLO";
System.out.println("s1.equalsIgnoreCase(s4):" + s1.equalsIgnoreCase(s4));
//3. 判断大字符串中是否包含小字符串
//boolean contains(String str);
String s5 = "helloworldhelloworld";
System.out.println("s5.contains(s1):"+s5.contains(s1));
//4. 判断字符串是否以某个指定的字符串开头
//boolean startWith(String str);
System.out.println("s5.startsWith(s1):"+s5.startsWith(s1));
System.out.println("s5.startsWith(s2):"+s5.startsWith(s2));
//5. 判断字符串是否以某个指定的字符串结尾
//boolean endsWith(String str);
System.out.println("s5.endsWith(s1): "+ s5.endsWith(s1));
System.out.println("s5.endsWith(s2): "+ s5.endsWith(s2));
//6. 判断字符串是否为空
//boolean isEmpty();
System.out.println("s5.isEmpty(): "+s5.isEmpty());
String s6 = "";
System.out.println("s6.isEmpty(): "+s6.isEmpty());
//String s7 = null;
//s7是一个空的指向,对一个对象做是否为空的判断,而s7没有创建对象,所以error
//System.out.println("s7.isEmpty(): "+s7.isEmpty());
}
}
String类的判断功能(方法)
最新推荐文章于 2022-04-22 18:02:26 发布