String的常用判斷功能

package niu.cheng2;


//String常用功能
//public boolean equals(Object anObject) //比較字符串內容是否相同,區分大小寫
//public boolean equalsIgnoreCase(String anotherString) //比較字符串內容是否相同,忽略大小寫
//public boolean contains(CharSequence s) //判斷大字符串中是否包含小字符串
//public boolean startsWith(String prefix) //判斷字符串是否以XX開頭的
//public boolean endsWith(String suffix) //判斷字符串是否以XX結尾的
//public boolean isEmpty() //判斷字符串是否爲空
//
//注意字符串對象爲空和字符串內容爲空時不同的
//String s="";字符串內容爲空
//String s=null;字符串對象爲空
public class StringDemo1 {
public static void main(String[] args) {
String s1="helloworld";
String s2="helloworld";
String s3="HelloWorld";
String s4="";
//public boolean equals(Object anObject) //比較字符串內容是否相同,區分大小寫
System.out.println("equals:"+s1.equals(s2));//equals:true
System.out.println("equals:"+s1.equals(s3));//equals:false
System.out.println("---------------------------");

//public boolean equalsIgnoreCase(String anotherString) //比較字符串內容是否相同,忽略大小寫
System.out.println("equalsIgnoreCase:"+s1.equalsIgnoreCase(s2));//equalsIgnoreCase:true
System.out.println("equalsIgnoreCase:"+s1.equalsIgnoreCase(s3));//equalsIgnoreCase:true
System.out.println("---------------------------");

//public boolean contains(CharSequence s) //判斷大字符串中是否包含小字符串
System.out.println("contains:"+s1.contains("hello"));//contains:true
System.out.println("contains:"+s1.contains("hw"));//contains:false
System.out.println("---------------------------");

//public boolean startsWith(String prefix) //判斷字符串是否以XX開頭的
System.out.println("startsWith:"+s1.startsWith("h"));//startsWith:true
System.out.println("startsWith:"+s1.startsWith("e"));//startsWith:false
System.out.println("---------------------------");

//public boolean endsWith(String suffix) //判斷字符串是否以XX結尾的
System.out.println("endsWith:"+s1.endsWith("d"));//endsWith:true
System.out.println("endsWith:"+s1.endsWith("w"));//endsWith:false
System.out.println("---------------------------");

//public boolean isEmpty() //判斷字符串是否爲空
System.out.println("isEmpty:"+s1.isEmpty());//isEmpty:false
System.out.println("isEmpty:"+s4.isEmpty());//isEmpty:true
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章