Last active
January 12, 2022 17:26
-
-
Save luisoos/0c4d38f547f29b5d92538f4f2c90802c to your computer and use it in GitHub Desktop.
Print the last word of a string in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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