只有一个参数的;

String str = new String("ABCD");
System.out.println("str="+str.substring(1));

进入substring()

public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = length() - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
if (beginIndex == 0) {
return this;
}
return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
: StringUTF16.newString(value, beginIndex, subLen);
}

分析:

  • 当 beginIndex < 0或者 beginIndex > length的时候,直接抛出越界异常;
  • 当 beginIndex 就是0 的时候,返回原字符串;
  • 最后判断是 isLatin1是否满足,进入StringLatin1/StringUTF16;

进入StringLatin1.newString

public static String newString(byte[] val, int index, int len) {
return new String(Arrays.copyOfRange(val, index, index + len),
LATIN1);
}

分析:

  • byte[] val : 也就是str的构成数组,{A,B,C,D};
  • int index:beginIndex = 1;
  • int len :subLen = 4 -1 = 3;

再调用 Arrays.copyOfRange

public static byte[] copyOfRange(byte[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
byte[] copy = new byte[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}

分析:

  • byte[] original:还是{A,B,C,D};
  • int from : index = 1;
  • int to :index + len = 1 + 3 = 4 (实际就是str的长度);
  • 该方法定义了一个新的数组,长度为:length - beginIndex = 3;

最后调用:

System.arraycopy

public static native void arraycopy(Object src,  int  srcPos,
Object dest, int destPos,
int length);

分析:

  • 参数1,src ,源数组 ,传入 original,{A,B,C,D};
  • 参数2 ,srcPos 源数组的开始位置,传入 beginIndex = 1;
  • 参数3, dest ,目标数组,传入是一个空的 length 为 3的数组;
  • 参数4,destPos 目标数组的开始位置,传入 0;
  • 参数5,length 需要copy元素的数量,本次调用,传递的是 length - beginIndex = 3(经过最小值判断,仍然等价)
  • 结果:
    • copy[0] = original[1];
    • copy[1] = original[2];
    • copy[2] = original[3];

综上,str.subString(i) 实际上是

  1. 将 str 转成数组 array01,赋值给一个为新的数组 array02,并且array02.length = str.length - i;
  2. 赋值过程为:array02[0] = array01[i](直到i = array01.length)
  3. array02转换新的String,并返回;

得出结论:subString(i)返回值是 str的索引位置i,到最大索引(两个索引都包括)

String类的subString(i)方法(基于jdk 1.9)的更多相关文章

  1. 【转载】C#中string类使用Substring方法截取字符串

    在C#的字符串操作过程中,截取字符串是一种常见的字符串操作,可使用string类的Substring方法来完成字符串的截取操作,该方法支持设定截取的开始位置以及截取的字符串长度等参数,Substrin ...

  2. Java——String类中的compareTo方法总结

    String类的定义:    java.lang  类 String   java.lang.Object      java.lang.String 所有已实现的接口:Serializable, C ...

  3. Java String类中的intern()方法

    今天在看一本书的时候注意到一个String的intern()方法,平常没用过,只是见过这个方法,也没去仔细看过这个方法.所以今天看了一下.个人觉得给String类中加入这个方法可能是为了提升一点点性能 ...

  4. String类中的equals()方法:

    String类中的equals()方法: public boolean equals(Object anObject) { //如果是同一个对象 if (this == anObject) { ret ...

  5. Java用代码演示String类中的以下方法的用法

    用代码演示String类中的以下方法的用法 (1)boolean isEmpty(): 判断字符串是不是空串,如果是空的就返回true (2)char charAt(int index): 返回索引上 ...

  6. Javascript String类的属性及方法

    String 类 Attribute and method anchor()              创建一个<a>标签的实例,将其name属性设置为被传递给此方法的字符串 big()  ...

  7. String类的常用判断方法使用练习

    选取了一些常用的判断方法进行了使用练习,后续跟新其他方法 package StringDemo; // String类的判断方法解析 // 1:boolean equals(); // 判断字符串是否 ...

  8. String类中一些的方法的应用

    一.整理string类 1.Length():获取字串长度: 2.charAt():获取指定位置的字符: 3.getChars():获取从指定位置起的子串复制到字符数组中:(它有四个参数) 4.rep ...

  9. String类中的equals()方法

    在Java中,每一个对象都有一个地址空间,在这空间保存着这个对象的值. equals 比较的是值,==比较的地址以及值. 01: public class StringExample02: {03: ...

随机推荐

  1. go-009-函数

    一.概述 Go 语言最少有个 main() 函数. 你可以通过函数来划分不同功能,逻辑上每个函数执行的是指定的任务. 函数声明告诉了编译器函数的名称,返回类型,和参数. Go 语言标准库提供了多种可动 ...

  2. 在eclipse中,Python项目遇到:…… from appium import webdriver ImportError: No module named appium

    1) Traceback (most recent call last):   File "D:\python workspace\src\p_test01\__init__.py" ...

  3. dbms_advisor 手动生成段顾问建议!

    执行包需要dbms_advisor权限: sys@ORCL> grant advisor to u1; 授权成功. 创建段顾问任务,指定create_task的advisor_name参数为“段 ...

  4. WebStorm keyboard shortcuts

    ctrl + D 向下复制 下面是Webstorm的一些常用快捷键: shift + enter: 另起一行 ctrl + alt + L: 格式化代码 control + E:  光标跳到行尾 it ...

  5. yii2引入js和css

    assets/AppAsset.php public $css = [ 'css/site.css', 'css/font/css/font-awesome.min.css', 'css/doc.cs ...

  6. Part01、sqlalchemy 使用

    一.ORM         连表               一对多               1.创建表,主动指定外键约束.               2.操作.                 ...

  7. The Jordan 3lab5 is the perfect sneaker for you

    The Jordan 5 3Lab5 Metallic Silver returns inside a mind-turning new iteration for that Spring/Summe ...

  8. httpfs的使用

    在项目中使用到hdfs作为存储,为了在不同的节点加载hdfs上的数据,我们使用nfsv3服务,在客户端使用 root来mount hdfs上的数据到本地,然后把本地的数据发到hdfs上,因为这个我们的 ...

  9. intellij-idea打包Scala代码在spark中运行

    .创建好Maven项目之后(记得添加Scala框架到该项目),修改pom.xml文件,添加如下内容: <properties> <spark.version></spar ...

  10. 运行bat时隐藏cmd窗口

    运行bat时隐藏cmd窗口 新建一个shrjj.vbs文件,文件内容为: Set ws = CreateObject("Wscript.Shell") ws.run "c ...