无论字符串大小如何,如何在 Java 中获取字符串中的最后一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3243721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:43:34  来源:igfitidea点击:

How to get the last characters in a String in Java, regardless of String size

javastring

提问by PuppyKevin

I'm looking for a way to pull the last characters from a String, regardless of size. Lets take these strings into example:

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

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

As you can see, completely random strings, but they have 7 numbers at the end. How would I be able to take those 7 numbers?

如您所见,完全随机的字符串,但它们最后有 7 个数字。我怎么能拿这7个数字?

Edit:

编辑:

I just realized that String[] string = s.split(": ");would work great here, as long as I call string[2] for the numbers and string[1] for anything in the middle.

我刚刚意识到这String[] string = s.split(": ");在这里会很好用,只要我调用 string[2] 表示数字,调用 string[1] 表示中间的任何内容。

采纳答案by Chris Dodd

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.

拆分所有数字序列;numbers 数组的最后一个元素可能就是您想要的。

回答by Jon Skeet

How about:

怎么样:

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

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

当然,这假设末尾7 个字符。如果您传递“12345”,它将引发异常。你可以这样解决:

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 textis null.

请注意,这仍然没有对结果字符串包含数字进行任何验证 - 如果text为空,它仍然会抛出异常。

回答by Prasoon Saurav

This should work

这应该工作

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

回答by FK82

I'd use either String.splitor a regex:

我会使用String.split或使用正则表达式:



Using String.split

使用 String.split

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


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

使用正则表达式(需要import java.util.regex.*

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

回答by Shehbaz Khan

This code works for me perfectly:

这段代码非常适合我:

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

回答by Sumanth

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.

如果输入字符串长度小于 7 ,请务必捕获 Exception

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

如果您想获得最后的“N”个字符,您可以用任何数字(例如 N)替换 7。

回答by Richard Inglis

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

这个问题是“Java String Right”的顶级谷歌结果。

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

令人惊讶的是,还没有人提到 Apache Commons StringUtils.right()

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

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

这也处理 wheretext为 null的情况,其中许多其他答案会抛出 NullPointerException。

回答by user2902302

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.

确保检查字符串长度大于 10。

回答by Agyeya Gupta

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

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

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

只要字符串的格式是固定的,您就可以使用substringAfterLast

回答by Géza

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.

给你答案。如果输入小于 7,则返回输入,如果 s == null,则返回 null。它从不抛出异常。

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

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