4

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

"aaple".lastSubstring(0, 1) = "e";
2
  • 1
    Err, what do 0 and 1 represent? And do you mean that e is returned?
    – Bart Kiers
    May 4, 2010 at 21:02
  • indexOf and lastIndexOf both take a string and find it without the main string, returning the location. You seem to be describing the opposite; you want a version of substring() that counts from the end instead of the beginning May 4, 2010 at 21:06

7 Answers 7

11

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

2
  • Great find. Who wrote commons-lang? May 6, 2010 at 6:04
  • Dead link. That's why SO generally frowns on answers that are just links.
    – SMBiggs
    Jun 15, 2019 at 18:15
4

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

s.substring(s.length()-endIndex,s.length()-beginIndex);
1
  • A nice thing of this implementation is that it runs in O(1) time. May 4, 2010 at 21:22
3

Perhaps lasIndexOf(String) ?

0

Wouldn't that just be

String string = "aaple";
string.subString(string.length() - 1, string.length());

?

1
  • 4
    Wouldn't that just be string.subString(string.length() - 1); ? May 4, 2010 at 21:05
0

You can use String.length() and String.length() - 1

0

For those looking to get a substring after some ending delimiter, e.g. parsing file.txt out of /some/directory/structure/file.txt

I found this helpful: 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")     = ""
-1

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().

10
  • s.substring(...) == "e" always returns false!
    – Bart Kiers
    May 4, 2010 at 21:09
  • Well, not always (the JVM can reuse strings, but it doesn't have to), but you're right. That's not the correct way to do it - I haven't been using enough Java lately... May 4, 2010 at 21:11
  • Yes, always (at least for all JVM's I ever used). substring(...) creates a new string so == will always return false. Only String literals inside .java files are pooled and re-used. The boolean x for the snippet String a = "foo"; String b = "foo"; boolean x = a == b; will be true.
    – Bart Kiers
    May 4, 2010 at 21:17
  • It's implementation-dependent and can vary from JVM to JVM, so you should not count on it, but it can happen. However, since it's an optimization, it's only likely to happen when doing so would likely have a performance benefit. And in this case, it's an unlikely optimization since it would be inefficient to always check new strings against existing ones, which is why it's unlikely to ever be true in this case. So, it's not surprising that you've never seen it in this sort of situation. String literals are a much more straightforward optimization, hence why they happen. May 4, 2010 at 21:21
  • Jonathan, did you miss my remark 'at least for all JVM's I ever used' ? And "Performance benefit"? "since it's an optimization"? Huh?
    – Bart Kiers
    May 4, 2010 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.