zoukankan      html  css  js  c++  java
  • JAVA中字符串函数subString的用法小结

    本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助

    String str; str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str;

    str=str.substring(int beginIndex,int endIndex);截取str中从beginIndex开始至endIndex结束时的字符串,并将其赋值给str;

    demo:

    复制代码 代码如下:
    class Test {  public static void main(String[] args)  {   String s1 ="1234567890abcdefgh";   s1 = s1.substring(10);   System.out.println(s1);  } }

    运行结果:abcdefgh

    复制代码 代码如下:
    class Test {  public static void main(String[] args)  {   String s1 ="1234567890abcdefgh";   s1 = s1.substring(0,9);   System.out.println(s1);  } }

    运行结果:123456789

    下面是个典型例子:

    复制代码 代码如下:
    public class StringDemo{

    public static void main(String agrs[]){
       String str="this is my original string";
       String toDelete=" original";
       if(str.startsWith(toDelete))     str=str.substring(toDelete.length());    else     if(str.endsWith(toDelete))      str=str.substring(0, str.length()-toDelete.length());     else     {      int index=str.indexOf(toDelete);      if(index!=-1)      {       String str1=str.substring(0, index);       String str2=str.substring(index+toDelete.length());       str=str1+str2;      }      else       System.out.println("string /""+toDelete+"/" not found");     }    System.out.println(str); } }

    运行结果: this is my string

  • 相关阅读:
    火狐获取图片宽和高的方法
    JDBC连接本地sqlserver2005的方法
    war文件不在tomcat 的webapps运行
    javascript 去除空格 方法
    火狐显示图片的方法
    八款开源Android游戏引擎
    android 模拟器 hardWare 属性说明
    jqgrid 属性说明
    Java命名规范
    在MyEclipse里怎样一次性取消所有断点
  • 原文地址:https://www.cnblogs.com/niceofday/p/5647392.html
Copyright © 2011-2022 走看看