13
String sentence = "Any simpler way to get the last element of a Java array?";
String lastToken = sentence.split(" ")[sentence.split(" ").length-1];

I'd like to split the sentence and get the last token. I feel my way of doing it is a little too awkward. Basically, I want the second statement to be shorter. Is that possible?

Edit: What I'm looking for: 1) no need to declare the array separately 2) no need to split the sentence twice. It would be good if there's a method called last with array. I suspect this is impossible but want to make sure.

Terry Li
  • 16,870
  • 30
  • 89
  • 134

4 Answers4

20

Another way to get the last token/word

String lastToken = sentance.replaceAll(".* ", "");
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    I found this one most elegant. Thanks. – Terry Li Mar 09 '13 at 23:29
  • 2
    +1, very elegant. Although I would use `sentance.trim().replaceAll(".* ", "");` to avoid empty String due to trailing whitespace. – jlordo Mar 09 '13 at 23:35
  • @jlordo - That has already been pointed out. However, what you are missing is that the original solution suffers from the same problem ... so you could view it as a *requirement* that any solution should behave the same way. – Stephen C Mar 09 '13 at 23:41
  • 2
    This solution is very error prone! "Hallo Welt!" => "Welt!" "Hallo Welt! " => "" . This is not elegant, this is dangerous! Split works in this case and also does the trim. – encc Feb 15 '17 at 07:36
15

You only need to split it once, and take the last element.

String sentence = "Any simpler way to get the last element of a Java array?";
String[] tokens = sentence.split(" ");
String lastToken = tokens[tokens.length-1];

It's awkward, but there's really no other way to do it unless you have foreknowledge of the length of the string.

Makoto
  • 104,088
  • 27
  • 192
  • 230
4
String sentence = "Any simpler way to get the last element of a Java array?";
String lastToken = sentence.substring( sentence.lastIndexOf(" ") + 1);
Akram Berkawy
  • 4,920
  • 2
  • 19
  • 27
  • This is quite specific to the string-situation though. It won’t work with an actual array, which was the real question… – poke Mar 09 '13 at 23:17
  • He meant array as in the array the string produces when split. –  Mar 09 '13 at 23:18
  • @Legend I know, but I understood that string splitting more as a way to get an example. – poke Mar 09 '13 at 23:18
4

If you are doing this just once, then Peter Lawrey's solution is shorter, though IMO it is harder to understand than the original version.

If you are doing this in multiple places then the following is better:

public String lastToken(String str, String separatorRegex) {
    String tokens[] = str.split(separatorRegex);
    return tokens[tokens.length - 1];
}

and then

String lastToken = lastToken(sentence, " ");

... which is more elegant than any clever hack ... IMO.


My more general point is that time spent trying to make a single line of code shorter is probably time wasted ... or worse:

  • From the perspective of SOMEONE ELSE reading your code, one lines versus two lines is irrelevant.
  • If the clever hack that makes the line shorter is obscure, then you have actually done a BAD THING by using it ... from the perspective of the next guy who reads / maintains your code.
  • If the clever hack is less efficient than the inelegant version, you may have introduced a performance issue.

But if you are repeating that line of code (or similar) in lots of places, then the best solution is to use procedural abstraction. Write a static or instance method ... once ... and call it multiple times.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216