Java 字符串截取函数 substring()

摘要:
如果起始值大于字符串的长度,则将引发越界异常;2: 字符串子字符串参数:beginIndex起始位置索引endIndex结束位置索引返回:从beginIndex位置到endIndex位置的字符串,例如:Stringstr=“hellowword!”;System.out.println;System.out.println;System.out.println;结果将是:elllohell如果startIndex和endIndex超出范围,则将引发超出范围的异常。

在String中有两个substring()函数,如下:

一:String.substring(int start)

参数:

    start:要截取位置的索引

返回:

   从start开始到结束的字符串

例如:String str = "hello word!";
         System.out.println(str.substring(1));

         System.out.println(str.substring(3));

   System.out.println(str.substring(6));

将得到结果为:

         ello word!

         lo word!

         ord!

如果start大于字符串的长度将会抛出越界异常;

二:String.substring(int beginIndex, int endIndex)

参数:

  beginIndex 开始位置索引

      endIndex    结束位置索引

返回:

      从beginIndex位置到endIndex位置内的字符串

例如:String str = "hello word!";

         System.out.println(str.substring(1,4));

         System.out.println(str.substring(3,5));

   System.out.println(str.substring(0,4));

将得到结果为:

        ell

        lo 

        hell

如果startIndex和endIndex其中有越界的将会抛出越界异常。

免责声明:文章转载自《Java 字符串截取函数 substring()》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇PHP调用Linux的命令行执行文件压缩命令&&创建文件夹修改权限Qt程序运行时出现Error下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

06 django视图层

视图层   视图就是一个函数,请求过来后要做什么事情都在这个函数里,也就是具体处理请求的功能。   熟练掌握两个视图层对象:请求对象(request)和响应对象(HttpResponse)。 HttpRequest对象 1 from django.shortcuts import render,redirect,HttpResponse 2 from...

String的intern方法的用处

今天第一次翻看Effective java,在其第一个item中讲静态工厂方法的有点的时候说到“它们每次被调用 的时候,不要非得创建一个新的对象”并在结尾处提到---"String.intern方法以一种有限的形式实现了这 种优化",由于以前没用过intern这个方法,所以去查了查,并把自己的理解写下来供以后参考首先来看其中文API的描述: intern...

DELPHI字符串、数组操作函数(转)

对字符串及数组的操作,是每个程序员必须要掌握的。熟练的使用Delphi的这些函数,在编程时能更加得心应手。   1.Copy   功能说明:该函数用于从字符串中复制指定范围中的字符。该函数有3个参数。第一个参数是数据源(即被复制的字符串),第二个参数是从字符串某一处开始复制,第三个参数是要复制字符串的长度(   即个数)。最后函数返回一个新的字符串(即是我...

java读取html文件,截取<body>标签中内容

1 public String readfile(String filePath){ 2 File file = new File(filePath); 3 InputStream input = null; 4 try { 5 input = new FileI...

身份证号码有效性检测算法 ( js版 转 C#版 )

C#版 #region 检测是否是正确的身份证 /// <summary> /// 身份证验证 /// </summary> /// <param name="num"></param> /// <returns></returns> public static bool isIdC...

安装包制作工具 SetupFactory使用2 API清单

SetupFactory中可以通过其API控制很复杂的业务需求。 下图中展示了其内置的API种类与具体分类函数。 序号 API名称 API说明 1 Application.Exit 退出安装程序,并返回一个可选的返回代码 2 Application.GetInstallLanguage 返回一个包含当前安装语言的表格 3 Applic...