zoukankan      html  css  js  c++  java
  • java用substring函数截取string中一段字符串

    在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其中有越界的将会抛出越界异常。

  • 相关阅读:
    window 操作
    idea使用
    安装zookeeper
    resource和autowired
    python浅见 (Python 3000)
    Tomcat服务器
    servlet
    事件是一种委托吗?什么是委托?什么是事件?
    int值类型的ToString()方法是否装箱
    抽象类,虚方法与普通类的区别
  • 原文地址:https://www.cnblogs.com/wangyayun/p/6755071.html
Copyright © 2011-2022 走看看