Java uses the substring function to intercept a string, substringstring
There are two substring () functions in String, as follows:
I. String. substring (int start)
Parameters:
Start: Index of the location to be intercepted
Return Value:
String from start to end
Example: String str = "hello word! ";
System. out. println (str. substring (1 ));
System. out. println (str. substring (3 ));
System. out. println (str. substring (6 ));
The result is as follows:
Ello word!
Lo word!
Ord!
If start is longer than the string length, an out-of-bounds exception is thrown;
Ii. String. substring (int beginIndex, int endIndex)
Parameters:
BeginIndex start Position Index
EndIndex end position index
Return Value:
String from beginIndex to endIndex
Example: String str = "hello word! ";
System. out. println (str. substring (1, 4 ));
System. out. println (str. substring (3,5 ));
System. out. println (str. substring (0, 4 ));
The result is as follows:
Ell
Lo
Hell
If either startIndex or endIndex is out of bounds, an out-of-bounds exception is thrown.