只有一个参数的;

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. linux下编写简单的守护进程

    搭建linux服务器的时候,需要写一个简单的守护进程来监控服务的运行情况,shell脚本如下: #!/bin/sh function daemon() { while true do server=` ...

  2. mysqldump 参数

    mysqldump是mysql用于转存储数据库的实用程序.它主要产生一个SQL脚本,其中包含从头重新创建数据库所必需的命令CREATE TABLE INSERT等.对于导出的文件,可使用SOURCE命 ...

  3. C# comport 打印图像

    public string GetLogo() { string logo = ""; if (!File.Exists(@"C:\bitmap.bmp")) ...

  4. 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 ...

  5. kubenetes 应用更新

    一.Deployment类型: 1.更新: 1).命令方式更新镜像: kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 k ...

  6. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) A. Andryusha and Socks

    地址:http://codeforces.com/contest/782/problem/A 题目: A. Andryusha and Socks time limit per test 2 seco ...

  7. Windows和Ubuntu双系统

    1  重装系统:Windows(Win7) 1.1 下载大白菜/老毛桃等工具,把U盘制作成启动盘 1.2 下载windows系统镜像文件放入U盘中 1.3  U盘插入待装系统的主机,开机进入BIOS( ...

  8. webmagic的设计机制及原理-如何开发一个Java爬虫 转

    此文章是webmagic 0.1.0版的设计手册,后续版本的入门及用户手册请看这里:https://github.com/code4craft/webmagic/blob/master/user-ma ...

  9. iOS 所有设备一览 && CoreFoundation源码

    1. 所有设备一览 https://en.wikipedia.org/wiki/List_of_iOS_devices 2. CoreFoundation源码(可以看看runloop.runtime的 ...

  10. JAVA学习笔记之JAVA 对象引用以及赋值

      关于对象与引用之间的一些基本概念. 初学Java时,在很长一段时间里,总觉得基本概念很模糊.后来才知道,在许多Java书中,把对象和对象的引用混为一谈.可是,如果我分不清对象与对象引用, 那实在没 ...