package cn.itcast_03;
/*code
1.boolean equals(Object obj):字符串的内容是否相同,区分大小写
2.boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
3.boolean contains(String str):判断大字符串中是否包含小字符串
4.boolean startsWith(String str):判断字符串是否以某个指定的字符串开始
5.boolean endsWith(String str):判断字符串是否以摸个指定的字符串结尾
6.boolean isEmpty():判断字符串是否为空
字符串为空和字符串对象为空不同。
String s = "";字符串为空
String s = null;字符串对象为空
public class StringDemo {对象
public static void main(String[] args) { //建立对象 String s1 = "helloworld"; String s2 = "helloworld"; String s3 = "HelloWorld"; String s4 = "hell"; //boolean equals(Object obj):字符串的内容是否相同,区分大小写 System.out.println("equals:" + s1.equals(s2));//true System.out.println("equals:" + s1.equals(s3));//false System.out.println("------------------------------------------------"); //boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 System.out.println("equalsIgnoreCase:" + s1.equalsIgnoreCase(s2));//true System.out.println("equalsIgnoreCase:" + s1.equalsIgnoreCase(s3));//true System.out.println("------------------------------------------------"); //boolean contains(String str):判断大字符串中是否包含小字符串 System.out.println("contains:" + s1.contains("hell"));//true System.out.println("contains:" + s1.contains("hw"));//false,字符必须是连在一块儿的 System.out.println("contains:" + s1.contains("owo"));//true System.out.println("contains:" + s1.contains(s4));//true System.out.println("------------------------------------------------"); //boolean startsWith(String str):判断字符串是否以某个指定的字符串开始 System.out.println("startsWith:" + s1.startsWith("h"));//true System.out.println("startsWith:" + s1.startsWith(s4));//true System.out.println("startsWith:" + s1.startsWith("world"));//false System.out.println("------------------------------------------------"); //boolean endsWith(String str):判断字符串是否以摸个指定的字符串结尾 System.out.println("endsWith:" + s1.endsWith("h"));//false System.out.println("endsWith:" + s1.endsWith(s4));//false System.out.println("endsWith:" + s1.endsWith("world"));//true System.out.println("------------------------------------------------"); //boolean isEmpty():判断字符串是否为空 System.out.println("isEmpty:" + s1.isEmpty());//false String s5 = ""; String s6 = null; System.out.println("isEmpty:" + s5.isEmpty());//true //对象都不存在,因此不能调用方法 System.out.println("isEmpty:" + s6.isEmpty());//NullPointerException }
}字符串