String类的常用的判断功能

String类的常用的判断功能:

boolean equals(Object obj):将此字符串与指定的对象比较
boolean equalsIgnoreCase(String str):将此字符串和另一个字符串进行比较,忽略大小写
boolean contains(String str):判断此字符串中是否包含str这个子字符串
boolean startsWith(String str):判断是否以str子字符串开头
boolean endsWith(String str):判断是否已str字符串结尾
boolean isEmpty():判断字符串是否为空

*注意:
String s = “” ;表示s字符串是空字符串
String s = null ;空对象,连对象都没有.

public class StringDemo {
    public static void main(String[] args) {
        //定义三个字符串
        String s1 = "helloworld" ;
        String s2 = "helloworld" ;
        String s3 = "HelloWorld" ;

//      boolean equals(Object obj):将此字符串与指定的对象比较 
        System.out.println(s1.equals(s2));//true
        System.out.println(s1.equals(s3));//false

        System.out.println("-------------------------");

//      boolean equalsIgnoreCase(String str):将此字符串和另一个字符串进行比较,忽略大小写
        System.out.println(s1.equalsIgnoreCase(s2));   //true
        System.out.println(s1.equalsIgnoreCase(s3));  //true

        System.out.println("-------------------------");

        //boolean contains(String str):判断此字符串中是否包含str这个子字符串
        System.out.println(s1.contains("h"));      //true
        System.out.println(s1.contains("hello"));   //true
        System.out.println(s1.contains("wor"));   // true 
        System.out.println("-------------------------");

        //boolean startsWith(String str):判断是否以str子字符串开头
        System.out.println(s2.startsWith("wor"));  //false
        System.out.println(s2.startsWith("h"));    //true

        System.out.println("-------------------------");

        //boolean endsWith(String str):判断是否已str字符串结尾 

        //boolean isEmpty():判断字符串是否为空
        System.out.println(s1.isEmpty());   //false

        String s4 = " "  ;//空格
        System.out.println(s4.isEmpty());   //false
        System.out.println("-------------------------");

        String s5 = null ;//NullPointerException:空指针异常
        //加入非空判断
        if(s5 !=null){
            System.out.println(s5.isEmpty());
        }       
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值