java:获取字符串中某个字符串第一次出现的位置(索引)

最近做项目遇到需要在文档中匹配特定的文字,在此做个备注;

下面代码是获取词的起始下标,可以用来进行词语匹配。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void main(String[] args) {
    String str = "人生自古谁无死,留取丹心照汗青";
    Integer start = startChar(str,"}");
    System.out.println(str.substring(start+1,str.length()));
}
 
public static Integer startChar(String str,String specialChar){
    Matcher matcher= Pattern.compile(specialChar).matcher(str);
    if(matcher.find()){
        System.out.println(matcher.start());
        Integer start = matcher.start();
        return start;
    }
    return null;
}

  转自:https://blog.csdn.net/weixin_38750084/article/details/106141034?utm_term=String%E6%9F%A5%E6%89%BE%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%BD%8D%E7%BD%AE%20java&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-0-106141034-null-null&spm=3001.4430

posted @   丹阳89  阅读(4628)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示