Java substring() 方法

时间:2020-07-19 00:58:03   收藏:0   阅读:314

substring() 方法返回字符串的子字符串。

语法:

参数:

返回值

子字符串。

例子

String str = "Hello World!";
//截取str子串,从下标为6(包括6)到字符串结束
System.out.println(str.substring(6));   //World!

//截取str子串,从下标为0(包括0)到下标为5(不包括5)
System.out.println(str.substring(0, 5));    //Hello

以下是相当于从头到尾截取字符串 :

String str = "Hello World!";

//Call to ‘substring(0)‘ is redundant(多余的)
System.out.println(str.substring(0));   //Hello World!

//Call to ‘substring(0, str.length())‘ is redundant(多余的)
System.out.println(str.substring(0, str.length())); //Hello World!
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!