java substring Method of Intercepting String

  • 2021-07-26 07:35:08
  • OfStack

substring (parameter) is one method of java to intercept a string.

It has two ways of transmitting parameters:

Type 1: public String substring (int beginIndex)

Returns a new string that is a substring of this string, starting with the specified indexed character and ending at the end of this string.

Type 2: public String substring (int beginIndex, int endIndex)

Also returns a new string that starts at the specified beginIndex index and ends at the specified endIndex index value.

Characters at the endIndex index are not included.

Therefore, the length of the string is endIndex-beginIndex.

Example 1:


public class Main {
  public static void main(String args[]) {
   String str = "this is Java";
	 String result = str.substring(8);
   System.out.println(result);
  }
}

Results: Java

Example 2:


public class Main {
  public static void main(String args[]) {
   String str = "this is Java";
	String result = str.substring(5,10);
   System.out.println(result);
  }
}

Results: is Ja


Related articles: