Given a string with a number of underscore characters, how can it be split into two substrings around the last underscore character?
eg. "a_b_c" => ["a_b", "_c"]
You can use lastIndexOf
on String
which returns you the index of the last occurrence of a chain of caracters.
String thing = "132131_12313_1321_312";
int index = thing.lastIndexOf("_");
String yourCuttedString = thing.substring(0, index);
It returns -1
if the occurrence is not found in the String.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With