Java substring() Method Guide: Extracting Substrings
In Java, the method used for string extraction is substring(). This method has two different forms of overloading.
- Extract a substring starting from the specified index position and return the substring from beginIndex to the end of the string.
- Return the substring of the string starting at the specified index and ending at endIndex-1.
The example code is as follows:
String str = "Hello World";
String sub1 = str.substring(6); // 从索引6开始截取字符串,返回"World"
String sub2 = str.substring(0, 5); // 从索引0到索引4截取字符串,返回"Hello"
Note: The substring() method returns a new string and does not modify the original string.