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
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章