您的位置 首页 java

java截取字符串后几位字符的方法

java截取字符串后几位字符的方法:

java中截取字符串中最后几个字符可以使用String类的substring方法,具体用法参考下方说明:

1、substring(int beginIndex)

substring(int beginIndex)

Returns a new string that is a substring of this string.截取

截取字符串位置beginIndex-1及以后的所有字符,注意字符的起始位置是从0开始的。

例:

String name = "weicc-20100107-00001";
System.out.println(name.substring(name.length()-5));
//输出00001

2、substring(int beginIndex, int endIndex)

substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string.

截取字符串位置beginIndex到位置endIndex-1的所有字符,此方法前闭后开的,即包括beginIndex位置的字符但是不包括endIndex位置的字符。

例:

String name="weicc-20100107-00001";
System.out.println(name.substring(name.length()-5,name.length()));
//输出00001

更多java知识请关注java基础教程栏目。

文章来源:智云一二三科技

文章标题:java截取字符串后几位字符的方法

文章地址:https://www.zhihuclub.com/20502.shtml

关于作者: 智云科技

热门文章

网站地图