Sring类的判断功能 API深入研究2.0

目录

String类的判断功能:boolean equals(Object obj)

boolean equalsIgnoreCase(String str)

boolean contains(String str)

boolean startsWith(String str)

boolean endsWith(String str)

boolean isEmpty()


点击下方链接学习更多的String类 API深入研究

字符串String API深入研究1.0

String类的获取功能 API深入研究3.0

String类的字符串转换功能 API深入研究4.0

String类的替换功能、去除字符串两端空格、按字典顺序比较两个字符串 API深入研究5.0


在我们企业的日常开发中,String类是每天都会去使用的高频类,那么我们来深入探索一下。(个人参考API文档编写,仅供学习参考)

字符串:简单理解,就是由多个字符组成的数据,叫做字符串;也可以看作是一个字符数组。

观察API发现:

1、String代表的是字符串,属于java.lang下面的。所以使用的时候不需要导包。

2、String类代表字符串,Java程序中的所有字符串文字(例如"abc"),都被实现为此类的实例(对象)。

3、字符串不变:它们的值在创建后不能被更改,字符串是常量,一旦被赋值,就不能改变。

注意事项:String类重写了toString()方法。
 


String类的判断功能:
boolean equals(Object obj)

查看API文档我们知道:

  将此字符串与指定对象进行比较。 其结果是true当且仅当该参数不是null并且是String对象,表示相同的字符序列作为该对象。

重写:

equalsObject

参数

anObject - 对比这个 String的对象

结果

true如果给定的对象代表一个 String等效于这个字符串,否则为false

顾名思义就是比较字符串的内容是否相等,区分大小写

参考代码:

public class testStringDemo1 {
    public static void main(String[] args) {
        String s1 = "helloworld";
        String s2 = "Helloworld";
        String s3 = "HelloWorld";
        String s4 = "helloworld";
        //boolean equals(Object obj) 比较字符串的内容是否相同 区分大小写
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s4));
    }
}

false
false
true

Process finished with exit code 0    


boolean equalsIgnoreCase(String str)

查看API文档我们知道:

  将此String与其他String比较,忽略案例注意事项。 如果两个字符串的长度相同,并且两个字符串中的相应字符等于忽略大小写,则两个字符串被认为是相等的。

如果以下至少一个为真,则两个字符c1c2被认为是相同的忽略情况:

参数

anotherString - String将此 String对比

结果

true如果参数不是null ,它代表等效的String忽略大小写; 否则为false

顾名思义就是比较字符串的内容是否相同,忽略大小写

参考代码:

public class testStringDemo1 {
    public static void main(String[] args) {
        String s1 = "helloworld";
        String s2 = "Helloworld";
        String s3 = "HelloWorld";
        String s4 = "helloworld";
        //boolean equalsIgnoreCase(String str)
        //比较字符串的内容是否相同,忽略大小写
        System.out.println(s1.equalsIgnoreCase(s2));
        System.out.println(s1.equalsIgnoreCase(s3));
        System.out.println(s1.equalsIgnoreCase(s4));
    }
}

true
true
true

Process finished with exit code 0


boolean contains(String str)

查看AIP文档我们知道:

当且仅当此字符串包含指定的char值序列时才返回true。

参数

s - 搜索的顺序

结果

如果此字符串包含 s ,则为true,否则为false

  顾名思义就是当且仅当此字符串包含指定的char值序列时才返回true;判断大的字符串中是否包含小的字符串,如果包含,返回true,如果不包含,返回false,区分大小写。

参考代码:

/*
    当且仅当此字符串包含指定的char值序列时才返回true。
    判断大的字符串中是否包含小的字符串,如果包含,返回true,反之false
    区分大小写
     */
public class testStringDemo1 {
    public static void main(String[] args) {
        String s1 = "helloworld";
        String s2 = "Helloworld";
        String s3 = "HelloWorld";
        String s4 = "helloworld";

        //boolean contains(String str)
        System.out.println(s1.contains("Hello"));
        System.out.println(s1.contains("leo"));
        System.out.println(s1.contains("hello"));
    }
}

false
false
true

Process finished with exit code 0


boolean startsWith(String str)

查看AIP文档我们知道:

测试此字符串是否以指定的前缀开头。

参数

prefix - 前缀。

结果

  true如果由参数表示的字符序列是由该字符串表示的字符序列的前缀; false否则。 还需要注意的是true如果参数为空字符串或等于该将被返回String如由所确定的对象equals(Object)方法。

顾名思义就是测试字符串是否以指定的前缀开头,区分大小写。

参考代码:

public class testStringDemo1 {
    public static void main(String[] args) {
        String s1 = "helloworld";
        String s2 = "Helloworld";
        String s3 = "HelloWorld";
        String s4 = "helloworld";

        //boolean startsWith(String str)
        //测试此字符串是否以指定的前缀开头,区分大小写
        System.out.println(s1.startsWith("hel"));
        System.out.println(s1.startsWith("h"));
        System.out.println(s1.startsWith("he"));
        System.out.println(s1.startsWith("he34"));
        System.out.println(s1.startsWith("H"));
    }
}

 true
true
true
false
false

Process finished with exit code 0


boolean endsWith(String str)

查看AIP文档我们知道:

测试此字符串是否以指定的后缀结尾。

参数

suffix - 后缀。

结果

true如果由参数表示的字符序列是由该对象表示的字符序列的后缀; false否则。 注意,结果将是true如果参数是空字符串或等于该String如由所确定的对象equals(Object)方法。

顾名思义就是测试此字符串是否以指定的后缀结束,区分大小写。

参考代码:

public class testStringDemo1 {
    public static void main(String[] args) {
        String s1 = "helloworld";
        String s2 = "Helloworld";
        String s3 = "HelloWorld";
        String s4 = "helloworld";

        //boolean endsWith(String str)
        //测试此字符串是否以指定的后缀结束,区分大小写
        System.out.println(s1.endsWith("orld"));
        System.out.println(s1.endsWith("orlD"));

    }
}

true
false

Process finished with exit code 0 


boolean isEmpty()

查看AIP文档我们知道:

退货 true如果,只有 length()0

结果

true如果 length()0 ,否则 false

顾名思义就是判断字符串是否是空字符串。

参考代码:

public class testStringDemo1 {
    public static void main(String[] args) {
        String s1 = "helloworld";
        String s2 = "Helloworld";
        String s3 = "HelloWorld";


        //boolean isEmpty() 判断字符串是否是空字符串
        System.out.println(s1.isEmpty());
        System.out.println("**************************");

        String s4 = "";
        String s5 = null;
        System.out.println(s4==s5);
        System.out.println(s4.isEmpty());
        //NullPointerException
//        System.out.println(s5.isEmpty());

    }
}

false
**************************
false
true

Process finished with exit code 0

 注意:boolean isEmpty()是判断是否为空字符串,如果字符串为空则返回true,反之false,千万不要搞混淆了!

String s6 = null;这样定义字符串是错的!否则报错NullPointerException空指针异常错误。

   字符串之间比较的要求,在不知道两个字符串变量的值的时候,为了防止 空指针异常,把变量放在后面。

参考代码:

/**
 *  字符串之间比较的要求,在不知道两个字符串变量的值的时候,为了防止
 *  空指针异常,把变量放在后面
 */
public class testStringDemo1 {
    public static void main(String[] args) {

        String s6 = "bigdata";
        String s7 = null;
//        System.out.println(s6.equals(s7));
//        System.out.println(s7.equals(s6));


        //需求:将s6,s7与"hadoop"进行比较
//        System.out.println(s6.equals("hadoop"));
//        System.out.println(s7.equals("hadoop"));


//      推荐写法
        System.out.println("hadoop".equals(s6));
        System.out.println("hadoop".equals(s7));
        System.out.println("bigdata".equals(s6));

    }
}

false
false
true

Process finished with exit code 0 

注意:在今后的字符串比较我们要把要比较的字符串写在前面,变量写在后。


到底啦!您收获到了什么呢?给靓仔点个关注吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liangzai2048

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值