Topic: 读core java时关于substring的疑问 |
Print this page |
1.读core java时关于substring的疑问 | Copy to clipboard |
Posted by: gumuyang Posted on: 2007-02-07 09:28 本人在看core java中文版(java2核心技术 卷1基础知识)第六版,读到47页关于substring的内容时有了疑问。 书上举了个例子: String greeting = "Hello"; String s = greeting.substring(0,4); 书上的解释是0就是起始位置,也就是Hello的H,后面的4就是第5个位置o,从第0位取到第4位,但不包含第4位。所以结果为Hell 我个人感觉不大对劲。 理由是:如果出现(3,2)这种情况作何解释?从第3位开始取到第2位? 所以我觉得greeting.substring(0,4)的意思应该是从初始位开始取4位字符。 不知是否正确?毕竟和书上不同,想向各位高人求证一下。 |
2.Re:读core java时关于substring的疑问 [Re: gumuyang] | Copy to clipboard |
Posted by: sanye303 Posted on: 2007-02-07 11:38 咱们可以测试一下嘛: public class strtest { public static void main(String[] args) { String str="123456"; String str2=str.substring(3,4); System.out.println(""+str2); } } 输出结果为4,如果String str2=str.substring(3,2)会报编译错误。substring()提供两个参数int beginindex和int endindex意义很明显了,起始位置和结束位置,而不是取endindex位字符。 |
3.Re:读core java时关于substring的疑问 [Re: gumuyang] | Copy to clipboard |
Posted by: gumuyang Posted on: 2007-02-07 12:21 原来如此,明白了,取起始位置和结束位置(但不包含结束位置) 感谢楼上用生动的例子让我一下子就能明白。 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |