Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last substring of string

Tags:

java

string

In Java we have indexOf and lastIndexOf. Is there anything like lastSubstring? It should work like :

"aaple".lastSubstring(0, 1) = "e";
like image 484
javaguy Avatar asked Dec 04 '22 12:12

javaguy


2 Answers

Not in the standard Java API, but ...

Apache Commons has a lot of handy String helper methods in StringUtils

... including StringUtils.right("apple",1)

http://commons.apache.org/lang/api/org/apache/commons/lang/StringUtils.html#right(java.lang.String,%20int)

just grab a copy of commons-lang.jar from commons.apache.org

like image 161
laher Avatar answered Dec 06 '22 02:12

laher


Generalizing the other responses, you can implement lastSubstring as follows:

s.substring(s.length()-endIndex,s.length()-beginIndex);
like image 21
Eyal Schneider Avatar answered Dec 06 '22 02:12

Eyal Schneider