String类的subString(i)方法(基于jdk 1.9)
只有一个参数的;
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) 实际上是
- 将 str 转成数组 array01,赋值给一个为新的数组 array02,并且array02.length = str.length - i;
- 赋值过程为:array02[0] = array01[i](直到i = array01.length)
- array02转换新的String,并返回;
得出结论:subString(i)返回值是 str的索引位置i,到最大索引(两个索引都包括)
String类的subString(i)方法(基于jdk 1.9)的更多相关文章
- 【转载】C#中string类使用Substring方法截取字符串
在C#的字符串操作过程中,截取字符串是一种常见的字符串操作,可使用string类的Substring方法来完成字符串的截取操作,该方法支持设定截取的开始位置以及截取的字符串长度等参数,Substrin ...
- Java——String类中的compareTo方法总结
String类的定义: java.lang 类 String java.lang.Object java.lang.String 所有已实现的接口:Serializable, C ...
- Java String类中的intern()方法
今天在看一本书的时候注意到一个String的intern()方法,平常没用过,只是见过这个方法,也没去仔细看过这个方法.所以今天看了一下.个人觉得给String类中加入这个方法可能是为了提升一点点性能 ...
- String类中的equals()方法:
String类中的equals()方法: public boolean equals(Object anObject) { //如果是同一个对象 if (this == anObject) { ret ...
- Java用代码演示String类中的以下方法的用法
用代码演示String类中的以下方法的用法 (1)boolean isEmpty(): 判断字符串是不是空串,如果是空的就返回true (2)char charAt(int index): 返回索引上 ...
- Javascript String类的属性及方法
String 类 Attribute and method anchor() 创建一个<a>标签的实例,将其name属性设置为被传递给此方法的字符串 big() ...
- String类的常用判断方法使用练习
选取了一些常用的判断方法进行了使用练习,后续跟新其他方法 package StringDemo; // String类的判断方法解析 // 1:boolean equals(); // 判断字符串是否 ...
- String类中一些的方法的应用
一.整理string类 1.Length():获取字串长度: 2.charAt():获取指定位置的字符: 3.getChars():获取从指定位置起的子串复制到字符数组中:(它有四个参数) 4.rep ...
- String类中的equals()方法
在Java中,每一个对象都有一个地址空间,在这空间保存着这个对象的值. equals 比较的是值,==比较的地址以及值. 01: public class StringExample02: {03: ...
随机推荐
- Java-小技巧-004-jdk时间,jdk8时间,joda,calendar,获取当前时间前一周、前一月、前一年的时间
1.推荐使用java8 localdate等 线程安全 支持较好 地址 2.joda 一.简述 查看SampleDateFormat源码,叙述有: * Date formats are not syn ...
- Storm程序永久代内存溢出
在集群中部署Storm应用程序的时候报错,并通过jdk自带的jconsole监控发现,永久代内存瞬间爆炸了 org.springframework.beans.factory.BeanCreation ...
- 避免SSH连接因超时闲置断开
用SSH过程连接电脑时,经常遇到长时间不操作而被服务器踢出的情况,常见的提示如: Write failed: Broken pipe 这是因为如果有一段时间在SSH连接上无数据传输,连接就会断开.解决 ...
- 84. Largest Rectangle in Histogram(直方图最大面积 hard)
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- AJAX POST请求中参数以form data和request payload形式在php中的获取方式
一.MINE TYPE问题: php对mime type为“application/x-www-form-urlencoded”(表单提交)和“multipart/form-data”(文件上传)的P ...
- Win32GUI代码示例
// Win32UI.cpp : 定义应用程序的入口点. // #include "stdafx.h" #include "Win32UI.h" #includ ...
- Linux进程管理 lsof命令:列出进程调用或打开的文件信息
lsof命令 通过 ps 命令查询到系统中所有的进程, 通过lsof 命令可以知道这个进程到底在调用哪些文件.lsof 命令格式如下: [root@localhost ~]# lsof [选项] 选项 ...
- FWT快速沃尔什变换
前言 学多项式怎么能错过\(FWT\)呢,然而这真是个毒瘤的东西,蒟蒻就只会背公式了\(\%>\_<\%\) 或卷积 \[\begin{aligned}\\ tf(A) = (tf(A_0 ...
- 分享基于Sails.js和React.js的全栈聊天室
在线地址: http://fiora.suisuijiang.com/ 项目源码[ 路过求star(^_^) ]: 后端: https://github.com/yinxin630/fiora-bac ...
- Linux安全基线检查脚本
基线检查内容: 一:共享账号检查 配置名称:用户账号分配检查,避免共享账号存在配置要求:1.系统需按照实际用户分配账号; 2.避免不同用户间共享账号,避免用户账号和服务器间通信使用的账号共享.操作指南 ...