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中方法重载和方法重写的区别 下一篇:无