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

Summary of usage of string function subString in JAVA

高洛峰
Release: 2017-01-18 16:48:37
Original
1899 people have browsed it

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);
 }
}
Copy after login

Run result: abcdefgh

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

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);
}
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template