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

java截取字符串后几位字符的方法,第1张

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://outofmemory.cn/langs/689222.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-21
下一篇 2022-04-21

发表评论

登录后才能评论

评论列表(0条)

保存