Java how to break a line from the end

I have a line like test.test.test ... ". Test", and I need to access the last "test" word on that line. Please note that the number of "tests" per line is unlimited. if java had a method similar to php function explode, everything was correct, but .... I think splitting from the end of the line can solve my problem. Is there a way to specify the direction for the method split? I know that one solution for this problem could be the following:

String parts[] = fileName.split(".");
//for all parts, while a parts contain "." character, split a part...

but I think this is a bad decision.

+4
source share
2 answers

Try a substring with the lastIndexOf method for String:

String str = "almas.test.tst";
System.out.println(str.substring(str.lastIndexOf(".") + 1));
Output:
tst
+7
source

, lastIndexOf(String str) .

String str = "test.test.test....test";

int pos = str.lastIndexOf("test");

String result = str.substring(pos);
+3

Source: https://habr.com/ru/post/1569861/


All Articles