String substring(int beginIndex, int endIndex)方法

String substring(int beginIndex, int endIndex)方法返回該字串的子字串。 子字串以指定索引處的字元開頭,並且如果給出第二個參數,則擴展到此字串的末尾或最多為endIndex - 1索引處之間的字元。

語法

以下是此方法的語法 -

public String substring(int beginIndex, int endIndex)

參數

  • beginIndex - 開頭索引(包括)。
  • endIndex - 結束索引(不包括)。

返回值

  • 指定beginIndex-endIndex之間的子字串。

示例

public class Test {

    public static void main(String args[]) {
        String Str = new String("Welcome to xuhuhu.com");

        System.out.print("Return Value :");
        System.out.println(Str.substring(11, 17));

    }
}

執行上面示例代碼,得到以下結果:

Return Value :zaixian

上一篇: java中方法重載和方法重寫的區別 下一篇:無