8

What is the easiest way to get every word in a string other than the last word in a string?

Up until now I have been using the following code to get the last word:

String listOfWords = "This is a sentence";
String[] b = listOfWords.split("\\s+");
String lastWord = b[b.length - 1];

And then getting the rest of the the string by using the remove method to remove the last word from the string.

I don't want to have to use the remove method. Is there a way similar to the above set of code to get the string of words without the last word and last space?

3
  • Do you want each word to be a separate string, or is one string containing everything but the last word ok, too?
    – jlordo
    Jan 15, 2013 at 10:30
  • I want every word other than the last word to be in 1 string, and then the last word to be in another string. Jan 15, 2013 at 10:31
  • 1
    use grouping and define a word as a group but also add a whitespace character after it. Something like (\w)\s Jan 15, 2013 at 10:33

6 Answers 6

23

Like this:

    String test = "This is a test";
    String firstWords = test.substring(0, test.lastIndexOf(" "));
    String lastWord = test.substring(test.lastIndexOf(" ") + 1);
8

You could get the lastIndexOf the white space and use a substring like below:

String listOfWords = "This is a sentence";
int index = listOfWords.lastIndexOf(" ");
System.out.println(listOfWords.substring(0, index));
System.out.println(listOfWords.substring(index+1));

Output:

        This is a
        sentence
0
4

Try using the method String.lastIndexOf in combination with String.substring.

String listOfWords = "This is a sentence";
String allButLast = listOfWords.substring(0, listOfWords.lastIndexOf(" "));
3

I added one line to your code. Nothing was removed here.

String listOfWords = "This is a sentence";
String[] b = listOfWords.split("\\s+");
String lastWord = b[b.length - 1];
String rest = listOfWords.substring(0, listOfWords.indexOf(lastWord)).trim(); // Added
System.out.println(rest);
2

This will suit your needs:

.split("\\s+[^\\s]+$|\\s+")

For example:

"This is a sentence".split("\\s+[^\\s]+$|\\s+");

Returns:

[This, is, a]
2
  • For some reason I keep getting: [Ljava.lang.String;@ca0b6 when I run the regex. Any ideas? Jan 15, 2013 at 11:00
  • @Digitalwolf Because split returns an array, and Ljava.lang.String;@ca0b6 is the representation of an array of strings (i.e. when calling myArray.toString()). Just use Arrays.asList(myArray) (e.g. Arrays.asList("This is a sentence".split("\\s+[^\\s]+$|\\s+"))) to get the result I provided, since a list has a better representation (more user-friendly let's say ;)).
    – sp00m
    Jan 15, 2013 at 11:06
2
public class StringArray {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String sentence = "this is a sentence";

        int index = sentence.lastIndexOf(" ");

        System.out.println(sentence.substring(0, index));

    }
}
1
  • An explanation would be in order. E.g., what is the idea/gist? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). Nov 1, 2021 at 22:53

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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