字符串的几种判断方法

时间:2025-03-21 07:09:48
String s = "China"; boolean na = s.endsWith("na");//判断一个字符串是否以某个字符串为后缀 System.out.println(na); String s1 = new String("china"); boolean equals = s.equals(s1); //判断两个字符串的值是否相等 System.out.println(equals); boolean b = s.equalsIgnoreCase(s1); //判断两个字符串忽略大小写是否相等,适合于验证码 System.out.println(b); boolean rld = s.contains("rld"); //判断一个字符串是否包含一个子字符串 System.out.println(rld); boolean hello = s.startsWith("Hello"); //判断是否是前缀开始 System.out.println(hello); boolean empty = "".isEmpty(); //判断一个字符串是否是空字符串 System.out.println(empty); boolean equals1 = "".equals(s); //判断一个字符串是否是空字符串 System.out.println(equals1);