Skip to content

Instantly share code, notes, and snippets.

@luisoos
Last active January 12, 2022 17:26
Show Gist options
  • Save luisoos/0c4d38f547f29b5d92538f4f2c90802c to your computer and use it in GitHub Desktop.
Save luisoos/0c4d38f547f29b5d92538f4f2c90802c to your computer and use it in GitHub Desktop.
Print the last word of a string in Java
String input = "This is an example with the string named input";
int position = input.lastIndexOf(" "); // Get last space
position++; // Jump to next character, so you skip the space
System.out.println(input.substring(position)); // Prints input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment