java中subSequence方法和java中的substringg方法的区别

Category:Java - Rosetta Code
Java This programming language may be used to instruct a computer to perform a task.
Compiled (bytecode)
Nominative
java, java5
Listed below are all of the tasks on Rosetta Code which have been solved using Java.
The Java programming language, developed by , is a language aimed at allowing "high-performance", virtual application development.
Java source files (.java files) are typically
to an intermediate
(all platform) executable (.class files) and executed by a . Most modern JVMs further compile the bytecode into your processor's native machine code during execution. This native code translation is mostly done via a
compiler that is built in the JVM. Some Java compilers (such as ) can compile Java code to native machine code ahead-of-time rather than just-in-time.
The primary benefits of Java are:
Cross- (Write Once, Run Anywhere (if all of the libraries are ported))
Comprehensive class library (which is thoroughly )
Large user community
Well supported by the
community and commercial industry (, , , HP, BEA, , etc.)
Java is used in a variety of environments including desktop applications, embedded device (PDA and wireless phone) applications, server-side web page serving applications (as ), and applets embedded in web pages.
There are a variety of arguments regarding Java's performance compared to other popular languages like . Some come to the conclusion that the programmer's choices make a bigger difference on performance in each language. Most show that each language is better than the other at specific types of operations (e.g. Java for memory allocation, C++ for numerical operations). An extensive summary of comparisons between Java and C++ can be found .
According to some , Java is currently one of the most popular programming languages in the world.
Useful Java links:
Subcategories
This category has the following 3 subcategories, out of 3 total.
Pages in category "Java"
The following 753 pages are in this category, out of 753 total.JDK6和JDK7中的substring()方法 - ImportNew
substring(int beginIndex, int endIndex)在JDK6与JDK7中的实现方式不一样,理解他们的差异有助于更好的使用它们。为了简单起见,下面所说的substring()指的就是substring(int beginIndex, int endIndex)方法。
1.substring()是做什么的?
substring(int beginIndex ,int endIndex)方法返回一个子字符串,返回的是从原字符串的beginIndex到endIndex-1之间的内容。
String x = &abcdef&;
x = x.substring(1,3);
System.out.println(x);
2.当substring()被调用的时候,内部发生什么事?
你或许会认为由于x是不可变的对象,当x被x.substring(1,3)返回的结果赋值后,它将指向一个全新的字符串如下图:
然而,这个图并不完全正确,或者说并没有完全表示出java 堆中真正发生的事情。那么当调用substring()的时候到底发生的了什么事呢?JDK 6与JDK7的substring方法实现有什么不一样呢?
3.JDK6中的substring()
java中字符串是通过字符数组来支持实现的,在JDK6中,String类包含3个域,char[] value、int offset、int count。分别用于存储真实的字符数组、数组的偏移量,以及String所包含的字符的个数。
当substring()方法被调用的时候,它会创建一个新的字符串对象,但是这个字符串的值在java 堆中仍然指向的是同一个数组,这两个字符串的不同在于他们的count和offset的值。
下面是jdk6中的原代码,是简化后只包含用来说明这个问题的关键代码:
String(int offset, int count, char value[]) {
this.value =
this.offset =
this.count =
public String substring(int beginIndex, int endIndex) {
//check boundary
new String(offset + beginIndex, endIndex - beginIndex, value);
4.jdk6中substring()将会导致的问题
如果你有一个非常长的字符串,但是你仅仅只需要这个字符串的一小部分,这就会导致性能问题(译注:可能会造成内存泄露,这个很早以前就有提及),因为你需要的只是很小的部分,而这个子字符串却要包含整个字符数组,在jdk6中解决办法就是使用下面的方法,它会指向一个真正的子字符串。
x = x.substring(x, y) + &&
5.JDK7中的substring()
在JDK7中有所改进,substring()方法在堆中真正的创建了一个新的数组,当原字符数组没有被引用后就被GC回收了.因此避免了上述问题.
public String(char value[], int offset, int count) {
//check boundary
this.value = Arrays.copyOfRange(value, offset, offset + count);
public String substring(int beginIndex, int endIndex) {
//check boundary
int subLen = endIndex - beginI
return new String(value, beginIndex, subLen);
译注: 在最新的Oralce JDK 6u45中,substring的实现方式已经改进了,不过我查到openjdk的就是原来的实现方式,有兴趣的可以查查具体是哪个版改进的.
原文链接:
- 译文链接: [ 转载请保留原文出处、译者和译文链接。]
关于作者:
程序员,关注 Java、Python、云计算,移动互联网。(新浪微博:)
写的很好的文章
关于ImportNew
ImportNew 专注于 Java 技术分享。于日 11:11正式上线。是的,这是一个很特别的时刻 :)
ImportNew 由两个 Java 关键字 import 和 new 组成,意指:Java 开发者学习新知识的网站。 import 可认为是学习和吸收, new 则可认为是新知识、新技术圈子和新朋友……
新浪微博:
推荐微信号
反馈建议:@
广告与商务合作QQ:
– 好的话题、有启发的回复、值得信赖的圈子
– 写了文章?看干货?去头条!
– 为IT单身男女服务的征婚传播平台
– 优秀的工具资源导航
– 活跃 & 专业的翻译小组
– 国内外的精选博客文章
– UI,网页,交互和用户体验
– JavaScript, HTML5, CSS
– 专注Android技术分享
– 专注iOS技术分享
– 专注Java技术分享
– 专注Python技术分享
& 2016 ImportNewJava中subsequence(0,1)是什么意思?_百度知道
Java中subsequence(0,1)是什么意思?
这是截取前两位字符的意思,其行为与String.subString一样。参考网站:
其他类似问题
为您推荐:
提问者采纳
或者s2是null值或s2是空字符串……s3 s4都是这样 都是或关系则结束方法.equals(&quot,1) 截取子串.trim()。f (s1 == null || s1; map = new HashMap&lt。这种方法其实你查一下API就完全OK的Map&lt.equals(&)) {return.equals(&String。说明Map键必须是S) ||s4 == nulls4, String&&quot,不过指定了泛型而已.trim(),或s1是空字符串;S&quot, String&&();) || s2 == null||s2,其他类型的数据不让往里传.trim();这就是new出一个HashMap的语句,值也必须是String.equals(&) ||s3 == null||s3;&quot.trim();}如果s1是null值subsequence(0
提问者评价
嗯 非常感谢
java的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁Java基础学习_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Java基础学习
上传于||文档简介
&&基​础​学​习
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
下载文档到电脑,查找使用更方便
还剩2页未读,继续阅读
你可能喜欢java中String的成员方法有哪些,为什么不建议频繁使用_百度知道
java中String的成员方法有哪些,为什么不建议频繁使用
提问者采纳
都会创建一个新的String对象,常用的有subString(),等等,indexOf.所以每次给String赋值或者更改值,造成不必要的浪费String的成员方法太多.因为String是一个不可变的
提问者评价
其他类似问题
为您推荐:
string的相关知识
其他1条回答
charAt(int index)
返回指定索引处的 char 值。
codePointAt(int index)
返回指定索引处的字符(Unicode 代码点)。
codePointBefore(int index)
返回指定索引之前的字符(Unicode 代码点)。
codePointCount(int beginIndex,
int endIndex)
返回此 String 的指定文本范围中的 Unicode
代码点数。
compareTo(String anotherString)
按字典顺序比较两个字符串。
compareToIgnoreCase(String str)
按字典顺序比较两个字符串,不考虑大小写。
concat(String str)
将指定字符串连接到...
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 java substring的用法 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信