Getting last word of a string
-
Working with string and i for something like this
String test = "this is one test string to show you";
now how do i fish out the last word of that string ?
-
Working with string and i for something like this
String test = "this is one test string to show you";
now how do i fish out the last word of that string ?
You could try
lastIndexOf()
string[^] to find the last space, and then usesubstring()
to extract the last part. There are other possibilities depending on your exact requirements. Reading the documentation will always help.Unrequited desire is character building. OriginalGriff
-
Working with string and i for something like this
String test = "this is one test string to show you";
now how do i fish out the last word of that string ?
System.out.println(test.substring(test.lastIndexOf(" ")==-1?0:test.lastIndexOf(" ")+1));
This one gives you the last word if there is a space, else the word itself if there is no space. Use trim beforehand in case if you expect spaces at the end!
-Shenbaga Murugan Paramasivapandian I hate computers and I just mess them up with my code!
-
Working with string and i for something like this
String test = "this is one test string to show you";
now how do i fish out the last word of that string ?
String test = "this is one test string to show you";
String[] words = test.split(" ");Then just get the last one in the array.
-
Working with string and i for something like this
String test = "this is one test string to show you";
now how do i fish out the last word of that string ?
Okay,u should get last index of space and last index of " first, then use substring(int beginIndex, int endIndex).