前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 源码 关于 字符串的包含 原来就是强行的匹配

Java 源码 关于 字符串的包含 原来就是强行的匹配

作者头像
韩旭051
发布2021-10-13 14:27:07
3530
发布2021-10-13 14:27:07
举报
文章被收录于专栏:刷题笔记刷题笔记

闲的没事 就是想看一下 Java 用的啥算法 查找 字符串匹配

String 的 contains 方法

代码语言:javascript
复制
 String s = new String("aaa");
        s.contains("a");

追到 String 源码 就是用的 indexOf 这个 方法

在这里插入图片描述
在这里插入图片描述

上源码

代码语言:javascript
复制
    static int indexOf(char[] source, int sourceOffset, int sourceCount,
            String target, int fromIndex) {
        return indexOf(source, sourceOffset, sourceCount,
                       target.value, 0, target.value.length,
                       fromIndex);
    }

转成 character 数组 进行 搜索🔍 可以 学到 target.value 用 String 转 数组

/** * Code shared by String and StringBuffer to do searches. The * source is the character array being searched, and the target * is the string being searched for. * * @param source the characters being searched. * @param sourceOffset offset of the source string. * @param sourceCount count of the source string. * @param target the characters being searched for. * @param targetOffset offset of the target string. * @param targetCount count of the target string. * @param fromIndex the index to begin searching from. */

代码语言:javascript
复制
  static int indexOf(char[] source, int sourceOffset, int sourceCount,
            char[] target, int targetOffset, int targetCount,
            int fromIndex) {
        if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
        }// 是不是 下标 和 长度 超了 直接返回
        if (fromIndex < 0) {
            fromIndex = 0;
        }// 从0以上开始才行
        if (targetCount == 0) {// 到头了就别 匹配了
            return fromIndex;
        }

        char first = target[targetOffset];// 开始匹配
        int max = sourceOffset + (sourceCount - targetCount);// 匹配终点

        for (int i = sourceOffset + fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (source[i] != first) {// 先匹配到 头节点
                while (++i <= max && source[i] != first);// 注意⚠️ for 循环♻️内部的 i跟着一起 变呢
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;// 第二个字符
                int end = j + targetCount - 1;// 最后的 范围
                for (int k = targetOffset + 1; j < end && source[j]
                        == target[k]; j++, k++);// 一个一个匹配 同时 更新 j++ 

                if (j == end) {  // ✅匹配成功 就返回
                    /* Found whole string. */
                    return i - sourceOffset;
                }
            }
        }
        return -1;// 🙅‍♂️ 到最后也没成功 ❌
    }

我以为多神奇的 匹配 原来就是 一个一个的匹配 哈哈 唯一的 优化 就是 先匹配第一个 字符 第一个字符 对上了后面就 循环遍历

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-08-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • String 的 contains 方法
    • 上源码
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档