Java 字符串的最后一个子串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2768931/
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
Last substring of string
提问by javaguy
In Java we have indexOf
and lastIndexOf
. Is there anything like lastSubstring
? It should work like :
在 Java 中,我们有indexOf
和lastIndexOf
。有什么类似的lastSubstring
吗?它应该像这样工作:
"aaple".lastSubstring(0, 1) = "e";
采纳答案by laher
Not in the standard Java API, but ...
不是在标准的 Java API 中,而是......
Apache Commons has a lot of handy String helper methods in StringUtils
Apache Commons 在 StringUtils 中有很多方便的字符串辅助方法
... including StringUtils.right("apple",1)
...包括 StringUtils.right("apple",1)
just grab a copy of commons-lang.jar from commons.apache.org
只需从 commons.apache.org 获取 commons-lang.jar 的副本
回答by ILMTitan
Wouldn't that just be
那不就是
String string = "aaple";
string.subString(string.length() - 1, string.length());
?
?
回答by vinaynag
You can use String.length() and String.length() - 1
您可以使用 String.length() 和 String.length() - 1
回答by Istao
Perhaps lasIndexOf(String)?
回答by Jonathan M Davis
I'm not aware of that sort of counterpart to substring()
, but it's not really necessary. You can't efficiently find the last index with a given value using indexOf()
, so lastIndexOf()
is necessary. To get what you're trying to do with lastSubstring()
, you can efficiently use substring()
.
我不知道 那种对应物substring()
,但这并不是真的必要。您无法使用 有效地找到具有给定值的最后一个索引indexOf()
,因此lastIndexOf()
是必要的。为了得到你想要做的事情lastSubstring()
,你可以有效地使用substring()
.
String str = "aaple";
str.substring(str.length() - 2, str.length() - 1).equals("e");
So, there's not really any need for lastSubstring()
.
所以,实际上没有任何必要lastSubstring()
。
回答by Eyal Schneider
Generalizing the other responses, you can implement lastSubstring as follows:
概括其他响应,您可以按如下方式实现 lastSubstring:
s.substring(s.length()-endIndex,s.length()-beginIndex);
回答by Ryan Walls
For those looking to get a substring after some ending delimiter, e.g. parsing file.txt
out of /some/directory/structure/file.txt
对于那些希望在某个结束分隔符之后获得子字符串的人,例如解析file.txt
出/some/directory/structure/file.txt
I found this helpful: StringUtils.substringAfterLast
我发现这很有帮助:StringUtils.substringAfterLast
public static String substringAfterLast(String str,
String separator)
Gets the substring after the last occurrence of a separator. The separator is not returned.
A null string input will return null. An empty ("") string input will return the empty string. An empty or null separator will return the empty string if the input string is not null.
If nothing is found, the empty string is returned.
StringUtils.substringAfterLast(null, *) = null
StringUtils.substringAfterLast("", *) = ""
StringUtils.substringAfterLast(*, "") = ""
StringUtils.substringAfterLast(*, null) = ""
StringUtils.substringAfterLast("abc", "a") = "bc"
StringUtils.substringAfterLast("abcba", "b") = "a"
StringUtils.substringAfterLast("abc", "c") = ""
StringUtils.substringAfterLast("a", "a") = ""
StringUtils.substringAfterLast("a", "z") = ""