载入中。。。 'S bLog
 
载入中。。。
 
载入中。。。
载入中。。。
载入中。。。
载入中。。。
载入中。。。
 
填写您的邮件地址,订阅我们的精彩内容:


 
Java中substring的注意点
[ 2011/6/7 20:30:00 | By: 梦翔儿 ]
 

梦翔儿今天调程序发现的:substring()的理解和一般不同,第一个参数是起始数组下标,第二个是要加上取几个字符后得到的值。而不是结束的字符,也不是取的个数。

先上代码,以截取固话区号及不含区号的号码为例:

  1 public class TestSubstring {

  2  public static void main(String[] args) {

  3   String phoneNbr="05718888888";

  4   //对于4位区号应该是substring(0,4),不是substring(0,3)

  5   System.out.println(phoneNbr.substring(0,4)+"   "+phoneNbr.substring(4));

  6  }

  7 }

  java API 中的说明:

  Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

  Examples:

  "hamburger".substring(4, 8) returns "urge"

  "smiles".substring(1, 5) returns "mile"

  Parameters:

  beginIndex the beginning index, inclusive.

  endIndex the ending index, exclusive.

 
 
  • 标签:Java substring 
  • 发表评论:
    载入中。。。

     
     
     

    梦翔儿网站 梦飞翔的地方 http://www.dreamflier.net
    中华人民共和国信息产业部TCP/IP系统 备案序号:辽ICP备09000550号

    Powered by Oblog.