String类的判断方法大全

 

  • boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
  • boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  • boolean contains(String str):判断大串中是否包含小串
  • boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
  • boolean endWith(String str):判断字符串是否以某个指定的字符串结尾
  • boolean isEmpty():判断字符串是否为空
  • public class Demo4_String {
    
    	public static void main(String[] args) {
    		String s1 = "manxue";
    		String s2 = "manxue";
    		String s3 = "ManXue";
    		String s4 = "我的名字是manxue";
    		String s5 = "我";
    		//不忽略大小写判断是否相同
    		System.out.println(s1.equals(s2));//true
    		System.out.println(s1.equals(s3));//false
    		//忽略大小写判断是否相同
    		System.out.println(s1.equalsIgnoreCase(s2));//true
    		System.out.println(s1.equalsIgnoreCase(s3));//true
    		//大串中有小串
    		System.out.println(s4.contains(s1));//true
    		System.out.println(s4.contains(s3));//false
    		//指定字符串开头
    		System.out.println("------------");
    		System.out.println(s4.startsWith("我"));//true
    		System.out.println(s4.startsWith(s5));//true
    		System.out.println(s4.startsWith(s1));//false
    		//指定字符串结尾
    		System.out.println(s4.endsWith(s5));//false
    		System.out.println(s4.endsWith(s1));//true
    		//字符串是否为空
    		System.out.println(s4.isEmpty());//false
    		System.out.println(s6.isEmpty());//true
    		System.out.println(s7.isEmpty());//报错,空指针异常
    	}
    
    }
    

     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不雨也潇潇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值