java检查两个字符串是否包含,java判断一个字符串是否包含另外一个字符串,java另外一个,要检查一个字符串是否包含...

java判断一个字符串是否包含另外一个字符串,java另外一个,要检查一个字符串是否包含

要检查一个字符串是否包含另外一个字符串需要使用indexOf()方法,如果indexOf方法返回大于等于0的值,则表示包含,若返回-1则表示字符串中不包含。

如下是示例代码:/** * Main.java * * @author byrx.net */public class Main { /** * 检查字符串是否包含某个字符串 */ public void checkStringContains() { String haystack = "Programming in Java"; String needle1 = "Java"; String needle2 = "Pascal"; int index1 = haystack.indexOf(needle1); int index2 = haystack.indexOf(needle2); if (index1 != -1) System.out.println("The string contains the substring " + needle1); else System.out.println("The string does not contain the substring " + needle1); if (index2 != -1) System.out.println("The string contains the substring " + needle2); else System.out.println("The string does not contain the substring " + needle2); } /** * Starts the program * * @param args the command line arguments */ public static void main(String[] args) { new Main().checkStringContains(); }}

上述代码的执行结果如下The string contains the substring JavaThe string does not contain the substring Pascal

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值