Home  >  Article  >  Java  >  Summary of usage of string function subString in JAVA

Summary of usage of string function subString in JAVA

高洛峰
高洛峰Original
2017-01-18 16:48:371828browse

String str;
str=str.substring(int beginIndex); intercept the string whose length is beginIndex from the first letter of str, and assign the remaining string to str;

str=str .substring(int beginIndex, int endIndex); Intercept the string from beginIndex to the end of endIndex in str, and assign it to str;

demo:

class Test 
{
 public static void main(String[] args) 
 {
  String s1 ="1234567890abcdefgh";
  s1 = s1.substring(10);
  System.out.println(s1);
 }
}

Run result: abcdefgh

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

Running result: 123456789

The following is a typical example:

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);
}
}

Running result:
this is my string

More characters in JAVA For related articles on the usage summary of the string function subString, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn