Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split by the last point of a String?

Tags:

java

string

split

I know I can split strings like this

String myString = "foo.bar";    
List<String> strings = Arrays.asList(myString.split("."));

But my String looks like 20151221.1051.Test.01.properties, it can have any number of dots, and I want to remove the .properties at the end (only by the last dot).

like image 592
Siro Duschletta Avatar asked Dec 22 '15 08:12

Siro Duschletta


1 Answers

Use myString.lastIndexOf(".") to get the index of the last dot.

For example, if you are sure that your input String contains at least one dot :

String name = myString.substring(0,myString.lastIndexOf("."));
like image 123
Eran Avatar answered Sep 22 '22 01:09

Eran