String substring(int beginIndex)

优质
小牛编辑
117浏览
2023-12-01

描述 (Description)

java.lang.String.substring(int beginIndex)方法返回一个新字符串,该字符串是此字符串的子字符串。 子字符串以指定索引处的字符开头,并延伸到此字符串的末尾。

声明 (Declaration)

以下是java.lang.String.substring()方法的声明

public String substring(int beginIndex)

参数 (Parameters)

beginIndex - 这是起始索引的值,包括。

返回值 (Return Value)

此方法返回指定的子字符串。

异常 (Exception)

IndexOutOfBoundsException - 如果beginIndex为负或大于此String对象的长度。

例子 (Example)

以下示例显示了java.lang.String.substring()方法的用法。

package cn.xnip;
import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str = "This is tutorials point";
      String substr = "";
      // prints the substring after index 7
      substr = str.substring(7);
      System.out.println("substring = " + substr);
      // prints the substring after index 0 i.e whole string gets printed
      substr = str.substring(0);
      System.out.println("substring = " + substr);
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

substring = tutorials point
substring = This is tutorials point