String常用方法解析

1、lastIndexOf(String str) 方法解析

 /**
     * 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.
     * 返回字符串最后一次出现的位置
     */
public int lastIndexOf(String str)

举例:

 public static void main(String[] args) {
        String string = "张三.txt";
        int length = string.length();
        System.out.println("string的长度是:" + length);
        int lastIndexOf = string.lastIndexOf(".");
        System.out.println("string中 . 最后出现的下标是:" + lastIndexOf);
}

输出结果是:

string的长度是:6
string中 . 最后出现的下标是:2

 

2、substring(int beginIndex)方法解析

此方法,常常配合lastIndexOf(String str) 方法使用

 /**
     * 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. 返回 截取开始下标(包括开始下标)后的所有字符串
     * @exception  IndexOutOfBoundsException  if
     *             {@code beginIndex} is negative or larger than the
     *             length of this {@code String} object.
     */
    public String substring(int beginIndex)

举例:获取文件的后缀

 public static void main(String[] args) {
        String string = "张三.txt";
        String suffix = string.substring((string.lastIndexOf(".")) + 1);
        System.out.println("文件后缀是:" + suffix);
 }

输出结果是:
文件后缀是:txt

3、substring(int beginIndex, int endIndex) 方法解析

    /**
     * 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. 结束下标(不包括)
     * @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) 

举例:获取文件的文件名

  public static void main(String[] args) {
        String string = "张三.txt";
        //不包含lastIndexOf下标
        String name = string.substring(0, string.lastIndexOf("."));
        System.out.println("文件名是:" + name);
}

输出结果是:
文件名是:张三

猜你喜欢

转载自blog.csdn.net/qq_39999478/article/details/107304584