如何在 Java 中获得字符串中的最后一个字符,而不管字符串的大小

我正在寻找一种方法,从字符串中提取最后的字符,不管大小。让我们以这些字符串为例:

"abcd: efg: 1006746"
"bhddy: nshhf36: 1006754"
"hfquv: nd: 5894254"

正如你所看到的,完全是随机的字符串,但是它们的末尾有7个数字。我怎么能接受这7个数字呢?

编辑:

我刚刚意识到,只要我把字符串[2]调用为数字,把字符串[1]调用为中间的任何内容,String[] string = s.split(": ");就可以很好地工作。

318356 次浏览

How about:

String numbers = text.substring(text.length() - 7);

That assumes that there are 7 characters at the end, of course. It will throw an exception if you pass it "12345". You could address that this way:

String numbers = text.substring(Math.max(0, text.length() - 7));

or

String numbers = text.length() <= 7 ? text : text.substring(text.length() - 7);

Note that this still isn't doing any validation that the resulting string contains numbers - and it will still throw an exception if text is null.

This should work

 Integer i= Integer.parseInt(text.substring(text.length() - 7));

Lots of things you could do.

s.substring(s.lastIndexOf(':') + 1);

will get everything after the last colon.

s.substring(s.lastIndexOf(' ') + 1);

everything after the last space.

String numbers[] = s.split("[^0-9]+");

splits off all sequences of digits; the last element of the numbers array is probably what you want.

I'd use either String.split or a regex:


Using String.split

String[] numberSplit = yourString.split(":") ;
String numbers = numberSplit[ (numberSplit.length-1) ] ; //!! last array element

Using RegEx (requires import java.util.regex.*)

String numbers = "" ;
Matcher numberMatcher = Pattern.compile("[0-9]{7}").matcher(yourString) ;
if( matcher.find() ) {
numbers = matcher.group(0) ;
}

This code works for me perfectly:

String numbers = text.substring(Math.max(0, text.length() - 7));

You can achieve it using this single line code :

String numbers = text.substring(text.length() - 7, text.length());

But be sure to catch Exception if the input string length is less than 7.

You can replace 7 with any number say N, if you want to get last 'N' characters.

This question is the top Google result for "Java String Right".

Surprisingly, no-one has yet mentioned Apache Commons StringUtils.right():

String numbers = org.apache.commons.lang.StringUtils.right( text, 7 );

This also handles the case where text is null, where many of the other answers would throw a NullPointerException.

String inputstr = "abcd: efg: 1006746"
int startindex = inputstr.length() - 10;
String outputtendigitstr = inputstr.substring(startindex);

Make sure you check string length is more than 10.

StringUtils.substringAfterLast("abcd: efg: 1006746", ": ") = "1006746";

As long as the format of the string is fixed you can use substringAfterLast.

org.apache.commons.lang3.StringUtils.substring(s, -7)

gives you the answer. It returns the input if it is shorter than 7, and null if s == null. It never throws an exception.

See https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#substring-java.lang.String-int-int-

E.g : "abcd: efg: 1006746" "bhddy: nshhf36: 1006754" "hfquv: nd: 5894254"

-Step 1: String firstString = "abcd: efg: 1006746";

-Step 2: String lastSevenNumber = firstString.substring(firstString.lastIndexOf(':'), firstString.length()).trim();