首页 > 编程语言 >Java源码阅读-String.startsWith(String prefix, int toffset)

Java源码阅读-String.startsWith(String prefix, int toffset)

时间:2024-04-24 20:46:03浏览次数:17  
标签:startsWith code return String value prefix 源码 toffset

/**
 * Tests if the substring of this string beginning at the
 * specified index starts with the specified prefix.
 *
 * @param   prefix    the prefix.
 * @param   toffset   where to begin looking in this string.
 * @return  {@code true} if the character sequence represented by the
 *          argument is a prefix of the substring of this object starting
 *          at index {@code toffset}; {@code false} otherwise.
 *          The result is {@code false} if {@code toffset} is
 *          negative or greater than the length of this
 *          {@code String} object; otherwise the result is the same
 *          as the result of the expression
 *          <pre>
 *          this.substring(toffset).startsWith(prefix)
 *          </pre>
 */
public boolean startsWith(String prefix, int toffset) {
    char ta[] = value;
    int to = toffset;
    char pa[] = prefix.value;
    int po = 0;
    int pc = prefix.value.length;
    // Note: toffset might be near -1>>>1.
    if ((toffset < 0) || (toffset > value.length - pc)) {
        return false;
    }
    while (--pc >= 0) {
        if (ta[to++] != pa[po++]) {
            return false;
        }
    }
    return true;
}

/**
 * Tests if this string starts with the specified prefix.
 *
 * @param   prefix   the prefix.
 * @return  {@code true} if the character sequence represented by the
 *          argument is a prefix of the character sequence represented by
 *          this string; {@code false} otherwise.
 *          Note also that {@code true} will be returned if the
 *          argument is an empty string or is equal to this
 *          {@code String} object as determined by the
 *          {@link #equals(Object)} method.
 * @since   1. 0
 */
public boolean startsWith(String prefix) {
    return startsWith(prefix, 0);
}

可以看到,两个版本的用法仅仅是在有无传入 toffest 这个参数上,或者说传入的 toffest 是否为 0
所以我们只需要弄明白第一个有 toffest 的版本就能理解这个方法

解读这个方法:

  1.value 是String对象的值,也就是我们对象本身所携带值,使用了一个 char[] 来接收这个值便于后续的使用

  2.to 记录 偏移量 或者说是 往后要对比的字符个数

  3.使用了 char[] pa 来接收我们传入的字符串的值便于后续使用

  4.po 作为一个遍历的下标

  5.比对个数不可能为零,且比对的个数不能比 被比字符串字符数-比对字符串的字符数 多  (toffest + pc > value.length)=>发生越界

        if ((toffset < 0) || (toffset > value.length - pc)) {
            return false;
        }

   6.从 被比字符串 的第 to 个(也就是第 toffest个) 和 pa 的第 po 个(也就是比对字符串 prefix 的第0个)做比较;同时对比的次数 -1

        while (--pc >= 0) {
            if (ta[to++] != pa[po++]) {
                return false;
            }
        }

 

 

标签:startsWith,code,return,String,value,prefix,源码,toffset
From: https://www.cnblogs.com/xxaxf/p/18156239

相关文章

  • JDK源码分析-ArrayList
    概述ArrayList是List接口的一个实现类,也是Java中最常用的容器实现类之一,可以把它理解为「可变数组」。Java中的数组初始化时需要指定长度,而且指定后不能改变。ArrayList内部也是一个数组,它对数组的功能做了增强:主要是在容器内元素增加时可以动态扩容,这也是ArrayList的......
  • lodash已死?radash最全使用介绍(附源码说明)—— Array方法篇(4)
    写在前面tips:点赞+收藏=学会!我们已经介绍了radash的相关信息和部分Array相关方法,详情可前往主页查看。本篇我们继续介绍radash中Array的相关方法的剩余方法。本期文章发布后,作者也会同步整理出Array方法的使用目录,包括文章说明和脑图说明。因为方法较多,后续将专门发布......
  • ToStringBuilder与直接toString的区别
    @OverridepublicStringtoString(){returnnewToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE).append("jobId",getJobId()).append("jobName",getJobName()).append("jobGroup&q......
  • 使用create-react-app,配置proxy报错(options.allowedHosts[0] should be a non-empty
    ​#使用create-react-app,配置proxy报错(options.allowedHosts[0]shouldbeanon-emptystring)今天在启动项目的时候遇到一个神奇的问题,这个问题具体报错信息是:Invalidoptionsobject.DevServerhasbeeninitializedusinganoptionsobjectthatdoesnotmatchtheAP......
  • 25-Mybatis源码分析
    1.架构设计&测试代码1.1Mybatis四层架构【API接口层】提供API增加、删除、修改、查询等接口,通过API接口对数据库进行操作;【数据处理层】主要负责SQL的查询、解析、执行以及结果映射的处理,主要作用解析SQL根据调用请求完成一次数据库操作;【框架支撑层】负责通用基......
  • [ABC343G] Compress Strings
    题目链接:https://www.luogu.com.cn/problem/AT_abc343_gsolution:1.首先我们将给出的字符串中互相包含的消去,可以使用kmp求前后缀来完成。和这道题的写法一样https://www.luogu.com.cn/problem/CF1200E2.我们发现给出的字符串最多只有20个,考虑状压来求解所有可能3.我们注意到这......
  • 18--Scrapy04--CrawlSpider、源码模板文件
    Scrapy04--CrawlSpider、源码模板文件案例:汽车之家,全站抓取二手车的信息来区分Spider和CrawlSpider注意:汽车之家的访问频率要控制一下,要不然会跳验证settings.py中设置DOWNLOAD_DELAY=3一、常规Spider#spiders/Ershou.pyimportscrapyfromscrapy.linkextra......
  • String.valueOf和强制类型转换(String)的区别
    String.valueOf和强制类型转换(String)在Java中都可以用来将其他类型的数据转换为字符串,但它们之间有一些重要的区别。空值处理:String.valueOf:当处理的对象为null时,String.valueOf会返回字符串"null",而不会抛出异常。(String):强制类型转换在面对null值时,会抛出NullPointerExc......
  • springboot源码:容器启动过程(扩展业务对象、bean 生命周期)&动态注册自己的业务对象&
    0.SpringbootRun方法启动org.springframework.boot.SpringApplication#run(java.lang.String...)启动 publicConfigurableApplicationContextrun(String...args){ longstartTime=System.nanoTime(); DefaultBootstrapContextbootstrapContext=createBootstrap......
  • CF1535F String Distance
    \(CF1535F\\String\Distance\)题意给\(n\)个长度均为\(len\)的字符串\(T_1,T_2,\dotsT_n\),定义\(f(a,b)\)为将\(a,b\)排序后相等的最小排序次数,若无解则为\(1337\)(这好像是个黑客用语)。求\[\sum_{i=1}^{n}\sum_{j=i+1}^{n}f(T_i,T_j)\]其中\[n\timeslen......