java 常用的字符串的判断方法

4:空字符串和null的区别
空字符串是有效的引用,是有地址的,只不过是内容的长度是0
null这个引用是空引用,不能使用.使用一定会报空指针的错误.引用数据类型的默认值是null
5:常用的字符串的判断方法
package cn.tx.demo;
public class StringTest1 {
public static void main(String[] args) {
String s = “helloworld”;
//判断一个字符串是否以某一个字符串为后缀如world
boolean world = s.endsWith(“ld”);
System.out.println(world);

    String s1 = new String("heLLoworld");
    //判断两个字符串的值是否相等;equals等号
    boolean equals = s.equals(s1);
    System.out.println(equals);

    //判断两个字符串忽略大小写后的值是否相等;equals等号,
    // 适合在验证码上应用
    boolean b = s.equalsIgnoreCase(s1);
    System.out.println(b);

    //判断一个字符串是否包含一个字符串
    boolean  s2 = s.contains("hellop");
    System.out.println(s2);

    //判断一个字符串是否以某一个字符串为前缀如world
    boolean s3 = s.startsWith("he");
    System.out.println(s3);
    //判断一个字符串是否是空串
    boolean s4 = "".isEmpty();
    System.out.println(s4);
    //判断一个字符串是否是空串,最好把空串的常量放在前面,
    // 防止空指针异常
    boolean s5 = "".equals(s);
    System.out.println(s5);
}

}

在这里插入图片描述

发布了103 篇原创文章 · 获赞 5 · 访问量 3061

猜你喜欢

转载自blog.csdn.net/weixin_45339692/article/details/103738168