java 匹配某个字符是否存在的几个方法

java 匹配某个字符是否存在,java.lang.String类中封装的3个常用的方法:

1.java.lang.String#contains

boolean    contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values.

2.java.lang.String#indexOf(java.lang.String) 

int    indexOf(int ch)
Returns the index within this string of the first occurrence of the specified character.

3.java.lang.String#startsWith(java.lang.String) 

boolean    startsWith(String prefix)
Tests if this string starts with the specified prefix.

DEMO:

/**
 * Copyright (C), 2000-2021, XXX有限公司
 * FileName: StringClassTest
 * Author: wangyetao
 * Date: 21-10-9 02:20:50
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 版本描述
 */
package simple.callback.test;

/**
 * @ClassName: StringClassTest
 * @Description: java类描述
 * @Author: wangyetao
 * @Date: 21-10-9 02:20:50
 */
public class StringClassTest {


    public static void main(String[] args) {
        /*String tstString = "今天 06:30-06:45";
        String tstString = "明天 06:30-06:45";*/
        String tstString = "10月10日 06:30-06:45";

        System.out.println(tstString.contains("今天"));
        System.out.println(tstString.contains("明天"));
        System.out.println(tstString.contains("月"));

        System.out.println(tstString.startsWith("今天"));
        System.out.println(tstString.startsWith("明天"));
        System.out.println(tstString.startsWith("月"));

        System.out.println(tstString.indexOf("今天"));
        System.out.println(tstString.indexOf("明天"));
        System.out.println(tstString.indexOf("月"));

    }
}

 DEMO输出:

false
false
true
false
false
false
-1
-1
2

Process finished with exit code 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dnbug Blog

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

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

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

打赏作者

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

抵扣说明:

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

余额充值