In java we have indexOf and lastIndexOf. Is there anythink like lastSubstring? It should work like :
"aaple".lastSubstring(0, 1) = "e";
In java we have indexOf and lastIndexOf. Is there anythink like lastSubstring? It should work like :
"aaple".lastSubstring(0, 1) = "e";
Won't this be:
public String lastSubstring(int startIdx, int endIdx) {
return str.substring(str.length() - endIdx, str.length() - startIdx);
}
Wouldn't that just be
String string = "aaple";
string.subString(string.length() - 1, string.length());
?
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().
String str = "aaple";
str.substring(str.length() - 2, str.length() - 1).equals("e");
So, there's not really any need for lastSubstring().
Generalizing the other responses, you can implement lastSubstring as follows:
s.substring(s.length()-endIndex,s.length()-beginIndex);
public string LastSubString(this string str) {
return str.Reverse().Substring(0,1);
}
Not in the standard Java API, but ...
Apache Commons has a lot of handy String helper methods in StringUtils
... including StringUtils.right("apple",1)
just grab a copy of commons-lang.jar from commons.apache.org