String的用法——判断功能

package cn.itcast_03;
/*

  • String的判断功能:
  •  1.boolean equals(Object obj):字符串的内容是否相同,区分大小写
  •  2.boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  •  3.boolean contains(String str):判断大字符串中是否包含小字符串
  •  4.boolean startsWith(String str):判断字符串是否以某个指定的字符串开始
  •  5.boolean endsWith(String str):判断字符串是否以摸个指定的字符串结尾
  •  6.boolean isEmpty():判断字符串是否为空
  • 注意:
  •  字符串为空和字符串对象为空不一样。
  •  String s = "";字符串为空
  •  String s = null;字符串对象为空
  • */

public class StringDemo {

public static void main(String[] args) {
    //创建对象
    String s1 = "helloworld";
    String s2 = "helloworld";
    String s3 = "HelloWorld";
    String s4 = "hell";
    
    //boolean equals(Object obj):字符串的内容是否相同,区分大小写
    System.out.println("equals:" + s1.equals(s2));//true
    System.out.println("equals:" + s1.equals(s3));//false
    System.out.println("------------------------------------------------");
    
    //boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
    System.out.println("equalsIgnoreCase:" + s1.equalsIgnoreCase(s2));//true
    System.out.println("equalsIgnoreCase:" + s1.equalsIgnoreCase(s3));//true
    System.out.println("------------------------------------------------");
    
    //boolean contains(String str):判断大字符串中是否包含小字符串
    System.out.println("contains:" + s1.contains("hell"));//true
    System.out.println("contains:" + s1.contains("hw"));//false,字符必须是连在一起的
    System.out.println("contains:" + s1.contains("owo"));//true
    System.out.println("contains:" + s1.contains(s4));//true
    System.out.println("------------------------------------------------");
    
    //boolean startsWith(String str):判断字符串是否以某个指定的字符串开始
    System.out.println("startsWith:" + s1.startsWith("h"));//true
    System.out.println("startsWith:" + s1.startsWith(s4));//true
    System.out.println("startsWith:" + s1.startsWith("world"));//false
    System.out.println("------------------------------------------------");
    
    //boolean endsWith(String str):判断字符串是否以摸个指定的字符串结尾
    System.out.println("endsWith:" + s1.endsWith("h"));//false
    System.out.println("endsWith:" + s1.endsWith(s4));//false
    System.out.println("endsWith:" + s1.endsWith("world"));//true
    System.out.println("------------------------------------------------");
    
    //boolean isEmpty():判断字符串是否为空
    System.out.println("isEmpty:" + s1.isEmpty());//false
    
    String s5 = "";
    String s6 = null;
    System.out.println("isEmpty:" + s5.isEmpty());//true
    //对象都不存在,所以不能调用方法
    System.out.println("isEmpty:" + s6.isEmpty());//NullPointerException
    
}

}

转载于:https://www.cnblogs.com/zhangyuestudying/p/10837107.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以使用两种方法来判断一个字符串是汉字还是字母。 第一种方法是使用字节码判断。根据引用\[1\]中的代码,我们可以定义三个方法来判断一个字符是中文、字母还是数字。isChinese方法判断一个字符是否是中文,isLetter方法判断一个字符是否是字母,isNumeric方法判断一个字符是否是数字。我们可以遍历字符串中的每个字符,使用这三个方法来判断每个字符的类型,如果所有字符都是中文,则字符串是汉字;如果所有字符都是字母,则字符串是字母;如果所有字符都是数字,则字符串是数字。 第二种方法是使用正则表达式判断。根据引用\[2\]中的代码,我们可以定义三个方法来判断一个字符串是纯汉字、纯字母还是纯数字。isChinese方法使用正则表达式判断一个字符串是否为纯汉字,isLetter方法使用正则表达式判断一个字符串是否为纯字母,isNumeric方法使用正则表达式判断一个字符串是否为纯数字。我们可以直接调用这三个方法来判断字符串的类型。 综上所述,我们可以使用以上两种方法来判断一个string类型是汉字还是字母。 #### 引用[.reference_title] - *1* *2* [每天记录学习的新知识 : 判断string或者char是中文、字母还是数字](https://blog.csdn.net/weixin_35691921/article/details/103250045)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [日常随笔——如何判断字符是汉字、字母、还是拼音? 在C++中又该如何判断?](https://blog.csdn.net/qq_44700810/article/details/125301871)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值