tags:

views:

143

answers:

8

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

"aaple".lastSubstring(0, 1) = "e";
A: 

Won't this be:

public String lastSubstring(int startIdx, int endIdx) {
    return str.substring(str.length() - endIdx, str.length() - startIdx);
}
Bozho
That would return "l" not "e".
ILMTitan
fixed the numbers, thanks.
Bozho
A: 

Wouldn't that just be

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

?

ILMTitan
Wouldn't that just be `string.subString(string.length() - 1);` ?
Chris Knight
A: 

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

vinaynag
A: 

Perhaps lasIndexOf(String) ?

Istao
A: 

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

Jonathan M Davis
`s.substring(...) == "e"` **always** returns false!
Bart Kiers
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...
Jonathan M Davis
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
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.
Jonathan M Davis
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
Yes I did see that part. Maybe I just wasn't clear enough. In any case, the only reason for a particular optimization is if it is actually a performance benefit (one type of optimization *can* hurt performance in other ways after all). My point was that the reason that you haven't seen this situation share string instances in any JVM that you've used is likely because optimizing the memory in that way would cost too much in terms of execution efficiency. So, the optimization would likely actually hurt performance rather than benefit it.
Jonathan M Davis
@Bart K - "for all JVM's I ever used" does not mean "always", let alone **"always"**. There are sure to be JVMs that you have never used, and some of them do not (did not) use one single line of Sun code.
Stephen C
@Stephen C, sure, that's why I specifically mentioned that fact in my second comment.
Bart Kiers
@Bart K - I know, I know, but it is *such fun* watching you try to avoid admitting that you were **wrong** :-)
Stephen C
@Stephen C, I give up, I give up! I admit I was **wrong** ! ... <mumble>sadist</mumble> :)
Bart Kiers
+1  A: 

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

s.substring(s.length()-endIndex,s.length()-beginIndex);
Eyal Schneider
A nice thing of this implementation is that it runs in O(1) time.
Eyal Schneider
A: 
public string LastSubString(this string str) {
    return str.Reverse().Substring(0,1);
}
soniiic
Parlez vous Java? :)
Eyal Schneider
That looks like a (pseudo-ish?) solution in C# (the OP was tagged with Java). If it was meant for Java, I recommend adhering to Java's naming conventions (method names start with a lower-case).
Bart Kiers
**1.** There is no Reverse() method on String. **2.** In the general case, that would presumably give you the reverse of the string that you want, so if you want to do it that way, you'd have to call Reverse() on the result. All in all, not the most efficient solution though.
Jonathan M Davis
+2  A: 

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

amir75
Great find. Who wrote commons-lang?
fastcodejava