判断方法
public class Demo4_StringMethod { /** * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写 * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 * boolean contains(String str):判断大字符串中是否包含小字符串 * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头 * boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾 * boolean isEmpty():判断字符串是否为空。 * * ""和null的区别 * ""是字符串常量,同时也是一个String类的对象,既然是对象当然可以调用String类中的方法 * null是空常量,不能调用任何的方法,否则会出现空指针异常,null常量可以给任意的引用数据类型赋值 */ public static void main(String[] args) { //demo1(); //demo2(); String s1 = "heima"; String s2 = ""; String s3 = null; System.out.println(s1.isEmpty()); System.out.println(s2.isEmpty()); System.out.println(s3.isEmpty()); //java.lang.NullPointerException } private static void demo2() { String s1 = "我爱heima,哈哈"; String s2 = "heima"; String s3 = "baima"; String s4 = "我爱"; String s5 = "哈哈"; System.out.println(s1.contains(s2)); //判断是否包含传入的字符串 System.out.println(s1.contains(s3)); System.out.println("------------------"); System.out.println(s1.startsWith(s4)); //判断是否以传入的字符串开头 System.out.println(s1.startsWith(s5)); System.out.println("------------------"); System.out.println(s1.endsWith(s4)); //判断是否以传入的字符串结尾 System.out.println(s1.endsWith(s5)); } private static void demo1() { String s1 = "heima"; String s2 = "heima"; String s3 = "HeiMa"; System.out.println(s1.equals(s2)); //true System.out.println(s2.equals(s3)); //false System.out.println("---------------"); System.out.println(s1.equalsIgnoreCase(s2)); System.out.println(s1.equalsIgnoreCase(s3)); //不区分大小写 }}
判断
public class Demo3_String { /** * * 1.判断定义为String类型的s1和s2是否相等 * String s1 = "abc"; * String s2 = "abc"; * System.out.println(s1 == s2); * System.out.println(s1.equals(s2)); * 2.下面这句话在内存中创建了几个对象? * String s1 = new String("abc"); * 3.判断定义为String类型的s1和s2是否相等 * String s1 = new String("abc"); * String s2 = "abc"; * System.out.println(s1 == s2); ? * System.out.println(s1.equals(s2)); ? * 4.判断定义为String类型的s1和s2是否相等 * String s1 = "a" + "b" + "c"; * String s2 = "abc"; * System.out.println(s1 == s2); ? * System.out.println(s1.equals(s2)); ? * 5.判断定义为String类型的s1和s2是否相等 * String s1 = "ab"; * String s2 = "abc"; * String s3 = s1 + "c"; * System.out.println(s3 == s2); * System.out.println(s3.equals(s2)); ? */ public static void main(String[] args) { //demo1(); //demo2(); //demo3(); //demo4(); String s1 = "ab"; String s2 = "abc"; String s3 = s1 + "c"; System.out.println(s3 == s2); System.out.println(s3.equals(s2)); //true } private static void demo4() { //byte b = 3 + 4; //在编译时就变成7,把7赋值给b,常量优化机制 String s1 = "a" + "b" + "c"; String s2 = "abc"; System.out.println(s1 == s2); //true,java中有常量优化机制 System.out.println(s1.equals(s2)); //true } private static void demo3() { String s1 = new String("abc"); //记录的是堆内存对象的地址值 String s2 = "abc"; //记录的是常量池中的地址值 System.out.println(s1 == s2); //false System.out.println(s1.equals(s2)); //true } private static void demo2() { //创建几个对象 //创建两个对象,一个在常量池中,一个在堆内存中 String s1 = new String("abc"); System.out.println(s1); } private static void demo1() { //常量池中没有这个字符串对象,就创建一个,如果有直接用即可 String s1 = "abc"; String s2 = "abc"; System.out.println(s1 == s2); //true System.out.println(s1.equals(s2)); //true }}