1. LastIndexOf(String str) method analysis
/**
* Returns the index within this string of the last occurrence of the
* specified substring. The last occurrence of the empty string ""
* is considered to occur at the index value {@code this.length()}.
*
* <p>The returned index is the largest value <i>k</i> for which:
* <blockquote><pre>
* this.startsWith(str, <i>k</i>)
* </pre></blockquote>
* If no such value of <i>k</i> exists, then {@code -1} is returned.
*
* @param str the substring to search for.要搜索的字符串
* @return the index of the last occurrence of the specified substring,
* or {@code -1} if there is no such occurrence.
* Returns the position of the last occurrence of the string
*/
public int lastIndexOf(String str)
For example:
public static void main(String[] args) { String string = "张三.txt"; int length = string.length(); System.out.println("The length of string is:" + length); int lastIndexOf = string.lastIndexOf("."); System.out.println("In string. The last subscript that appears is: "+ lastIndexOf); } The output result is: The length of string is: 6 in string. The last subscript that appears Yes: 2
2. Analysis of substring(int beginIndex) method
This method is often used in conjunction with the lastIndexOf(String str) method
/** * Returns a string that is a substring of this string. The * substring begins with the character at the specified index and * extends to the end of this string. <p> * Examples: * <blockquote><pre> * "unhappy".substring(2) returns "happy" * "Harbison".substring(3) returns "bison" * "emptiness".substring(9) returns "" (an empty string) * </pre></blockquote> * * @param beginIndex the beginning index, inclusive. 截取的开始下标(包括开始下标) * @return the specified substring. Returns all strings after the start index (including the start index) is intercepted * @exception IndexOutOfBoundsException if * {@code beginIndex} is negative or larger than the * length of this {@code String} object. */ public String substring(int beginIndex)
Example: Get the suffix of a file
public static void main(String[] args) { String string = "张三.txt"; String suffix = string.substring((string.lastIndexOf(".")) + 1); System.out.println("File The suffix is: "+ suffix); } The output result is: the file suffix is: txt
3. Analysis of substring(int beginIndex, int endIndex) method
/** * Returns a string that is a substring of this string. The * substring begins at the specified {@code beginIndex} and * extends to the character at index {@code endIndex - 1}. * Thus the length of the substring is {@code endIndex-beginIndex}. * <p> * Examples: * <blockquote><pre> * "hamburger".substring(4, 8) returns "urge" * "smiles".substring(1, 5) returns "mile" * </pre></blockquote> * * @param beginIndex the beginning index, inclusive. 开始下标(包括) * @param endIndex the ending index, exclusive. End subscript (not included) * @return the specified substring. * @exception IndexOutOfBoundsException if the * {@code beginIndex} is negative, or * {@code endIndex} is larger than the length of * this {@code String} object, or * {@code beginIndex} is larger than * {@code endIndex}. */ public String substring(int beginIndex, int endIndex)
Example: Get the file name of the file
public static void main(String[] args) { String string = "张三.txt"; //Does not include lastIndexOf subscript String name = string.substring(0, string.lastIndexOf(".")); System.out .println("The file name is:" + name); }The output result is: the file name is: Zhang San