栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 匹配某个字符是否存在

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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:

package simple.callback.test;


public class StringClassTest {


    public static void main(String[] args) {
        
        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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/305583.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号