当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
Java中substring的注意点
发布时间:2010/12/28 9:29:26 来源:城市学习网 编辑:ziteng

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

  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.

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved