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
}
}
//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
}
}
本文详细介绍了 Java 中 String 类的几个常用方法:equals 方法用于比较字符串内容是否相同且区分大小写;equalsIgnoreCase 方法用于比较字符串内容是否相同但不区分大小写;contains 方法用于判断大字符串中是否包含小字符串;startsWith 和 endsWith 方法分别用于判断字符串是否以特定字符开头或结尾;isEmpty 方法用于判断字符串是否为空。
864

被折叠的 条评论
为什么被折叠?



