zoukankan      html  css  js  c++  java
  • String类之substring--->查找某位置对应的字

    以下方法都是java内置类String类的内置方法(不是构造方法哦,就是普通的方法),不需要我们写,直接拿过来用即可。

    substring方法对应Api介绍

     

    查找字符串中的 从int beginIndex及其以后的字符串:substring(int beginIndex)

    public class Demo {
        public static void main(String[] args)  {
            String Str="MyNameIsDsh";
            String substr=Str.substring(8);//截取字符串Str第8位之后的字符
            System.out.println("截取后的新字符串是:"+substr);
        }
    }

    截取后的新字符串是:Dsh

    查找字符串中的 从int beginIndex-int endIndex之间的字符串:substring(int beginIndex,int endIndex)

    public class Demo {
        public static void main(String[] args)  {
            String Str="MyNameIsDsh";
            String substr=Str.substring(8,10);//截取字符串Str第8-10位之间的字符串
            System.out.println("截取后的新字符串是:"+substr);
        }
    }

    截取后的新字符串是:Ds

    如果超出查找字符串本身长度核报异常:String index out of range

  • 相关阅读:
    linux安装lamp/lamp/lanmp
    git命令
    redis常问面试题
    liunx 项目发布(django + uwsgi + nginx+supervisor发布web服务器)
    安装nginx
    liunx安装mysql(mariadb)
    linux安装python3
    scrapy框架day01
    网络编程, socket用法
    面向对象进阶
  • 原文地址:https://www.cnblogs.com/dshvv/p/5096850.html
Copyright © 2011-2022 走看看