String的常用判断功能

本文详细介绍了 Java 中 String 类的几个常用方法:equals 方法用于比较字符串内容是否相同且区分大小写;equalsIgnoreCase 方法用于比较字符串内容是否相同但不区分大小写;contains 方法用于判断大字符串中是否包含小字符串;startsWith 和 endsWith 方法分别用于判断字符串是否以特定字符开头或结尾;isEmpty 方法用于判断字符串是否为空。
package niu.cheng2;


//String常用功能
//public boolean equals(Object anObject) //比较字符串内容是否相同,区分大小写
//public boolean equalsIgnoreCase(String anotherString) //比较字符串内容是否相同,忽略大小写
//public boolean contains(CharSequence s) //判断大字符串中是否包含小字符串
//public boolean startsWith(String prefix) //判断字符串是否以XX开头的
//public boolean endsWith(String suffix) //判断字符串是否以XX结尾的
//public boolean isEmpty() //判断字符串是否为空
//
//注意字符串对象为空和字符串内容为空时不同的
//String s="";字符串内容为空
//String s=null;字符串对象为空
public class StringDemo1 {
public static void main(String[] args) {
String s1="helloworld";
String s2="helloworld";
String s3="HelloWorld";
String s4="";
//public boolean equals(Object anObject) //比较字符串内容是否相同,区分大小写
System.out.println("equals:"+s1.equals(s2));//equals:true
System.out.println("equals:"+s1.equals(s3));//equals:false
System.out.println("---------------------------");

//public boolean equalsIgnoreCase(String anotherString) //比较字符串内容是否相同,忽略大小写
System.out.println("equalsIgnoreCase:"+s1.equalsIgnoreCase(s2));//equalsIgnoreCase:true
System.out.println("equalsIgnoreCase:"+s1.equalsIgnoreCase(s3));//equalsIgnoreCase:true
System.out.println("---------------------------");

//public boolean contains(CharSequence s) //判断大字符串中是否包含小字符串
System.out.println("contains:"+s1.contains("hello"));//contains:true
System.out.println("contains:"+s1.contains("hw"));//contains:false
System.out.println("---------------------------");

//public boolean startsWith(String prefix) //判断字符串是否以XX开头的
System.out.println("startsWith:"+s1.startsWith("h"));//startsWith:true
System.out.println("startsWith:"+s1.startsWith("e"));//startsWith:false
System.out.println("---------------------------");

//public boolean endsWith(String suffix) //判断字符串是否以XX结尾的
System.out.println("endsWith:"+s1.endsWith("d"));//endsWith:true
System.out.println("endsWith:"+s1.endsWith("w"));//endsWith:false
System.out.println("---------------------------");

//public boolean isEmpty() //判断字符串是否为空
System.out.println("isEmpty:"+s1.isEmpty());//isEmpty:false
System.out.println("isEmpty:"+s4.isEmpty());//isEmpty:true
}
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值