String类的substring方法
下列程序的输出是什么?
class A { public static void main(String[] a) {
String v = “base”; v.concat(“ball”);
v.substring(1,5); System.out.println(v); }
}
分析:由于String是不可变的,当执行v.concat("ball")时,v本身还是指向“base”,当再执行v.substring(1,5)时,会报出界异常
如下为concat的源码:可以看到,最后返回的是新的字符串
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
如下是substring源码:
public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
} public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
String类的substring方法的更多相关文章
- 【转载】C#中string类使用Substring方法截取字符串
在C#的字符串操作过程中,截取字符串是一种常见的字符串操作,可使用string类的Substring方法来完成字符串的截取操作,该方法支持设定截取的开始位置以及截取的字符串长度等参数,Substrin ...
- String类的substring()方法
截取字符串,在java语言中的用法 1. public String substring(int beginIndex) 返回一个新字符串,它是此字符串的一个子字符串.该子字符串始于指定索引处的字符 ...
- 关于JAVA的String类的一些方法
一.得到字符串对象的有关信息 1.通过调用length()方法得到String的长度. String str=”This is a String”; int len =str.length(); 2. ...
- C++ string 类的 find 方法实例详解
1.C++ 中 string 类的 find 方法列表 size_type std::basic_string::find(const basic_string &__str, size_ty ...
- java.lang.String 类的所有方法
java.lang.String 类的所有方法 方法摘要 char charAt(int index) 返回指定索引处的 char 值. int codePointAt(int index) 返回指定 ...
- String类的indexOf方法的用法和举例
2017年3月3号博主第一次去郑州互联网公司面试,背景是这样的我先前去了农大龙子湖校园招聘投简历,然后第二天去面试了那经历可以说是很失败的一次面试,当然这跟自己的水平有关了接下来重点讲一下面试的题目: ...
- String类的常见方法的使用案例
String类的常见方法的使用案例 //使用指定的字符串替换当前字符串中指定的内容 //将helloworld中的o替换为a String s="HelloWorld"; Stri ...
- Java中String类的format方法使用总结
可参考: http://www.cnblogs.com/fsjohnhuang/p/4094777.html http://kgd1120.iteye.com/blog/1293633 String类 ...
- String类的split方法以及StringTokenizer
split方法可以根据指定的表达式regex将一个字符串分割成一个子字符串数组. 它的参数有两种形式,也即:split(String regex)和split(String regex, int li ...
随机推荐
- java写入换行符
写入一个文件,生成文本文档,里面写入1000行字符,但是写出来的没有换行.所以纠结,百度了下,一行完事. String crlf=System.getProperty("line.separ ...
- 深夜配置一把struts2
在intellij idea里面配置出来了struts2的一个Helloworld,因为换了工具,在网上查了很多关于IDEA配置它的方式,好多是用Maven解决依赖关系的.于是按照网上的来,发现很多东 ...
- Tomcat不自动解压问题
问题: 版本迭代上线,需要更换新的war包, 1.先将老的war和文件夹删除掉,再放入新的war到webapps中, 2.发现启动Tomcat后没有解压该war包, 3.需要先将server.xml中 ...
- LOJ121 【离线可过】动态图连通性
题目链接:戳我 [线段树分治版本代码] 这里面的线段树是时间线段树,每一个节点都要开一个vector,记录当前时间区间中存在的边的标号qwq #include<iostream> #inc ...
- AVA + Spectron + JavaScript 对 JS 编写的客户端进行自动化测试
什么是 AVA (类似于 unittest) AVA 是一种 JavaScript 单元测试框架,是一个简约的测试库.AVA 它的优势是 JavaScript 的异步特性和并发运行测试, 这反过来提高 ...
- “全栈2019”Java异常第十八章:Exception详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- vue 路由传参
mode:路由的形式 用的哪种路由 1.hash 路由 会带#号的哈希值 默认是hash路由 2.history路由 不会带#的 单页面开发首屏加载慢怎么解决?单页面开发首屏加载白屏怎 ...
- 使用 Hexo + github 搭建个人博客
来自:http://www.cnblogs.com/fengzheng/p/8031518.html Hexo 是什么 Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或 ...
- (C/C++) Array 印出所有排列組合
#include <stdio.h> #include <stdlib.h> #define N 4 , , , }; void swap(int *a, int *b) { ...
- python 全栈开发:基础复习
格式化输出: username = 'python' password = 'python1' print(''' username:%s password:%s '''%(username,pass ...