java substring 截取字符串的方法

2026-01-12 10:02:02 作者:用户投稿

substring(参数)是java截取字符串的一个方法。

它有两种传参的方式:

第一种:public String substring(int beginIndex)

返回一个新的字符串,它是此字符串的一个子字符串,该字符串从指定索引出的字符开始,到此字符串末尾结束。

第二种:public String substring(int beginIndex,int endIndex)

同样返回一个新的字符串,该字符串从指定的beginIndex索引处开始,到指定的endIndex索引值结束。

不包括endIndex索引处的字符。

所以,该字符串的长度就是endIndex-beginIndex。

示例一:

public class Main {
  public static void main(String args[]) {
   String str = "this is Java";
	 String result = str.substring(8);
   System.out.println(result);
  }
}

结果: Java

示例二:

public class Main {
  public static void main(String args[]) {
   String str = "this is Java";
	String result = str.substring(5,10);
   System.out.println(result);
  }
}

结果:is Ja

以上所述是小编给大家介绍的java substring 截取字符串的方法详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

猜你喜欢

联络方式:

400 9058 355

邮箱:8955556@qq.com

Q Q:8955556

微信二维码
在线咨询 拨打电话

电话

400 9058 355

微信二维码

微信二维码