java - get 2 characters from a string -


i have string filename holds file names in foreach loop. string like:

myfile_test_india_20160728 myfile_test_america_20160728     myfile_test_germany_20160728 

i need first 2 characters of country name. tried below:

string rmtdir = filename.substring(filename.length() - 12, filename.length() - 12); system.out.println(rmtdir);  

but using required data india.
other countries, manually need update 2nd part of substring, keeping extended length of countries in mind.

like america , germany:

string rmtdir = filename.substring(filename.length() - 12, filename.length() - 14); 

is there way go starting index , select number of positions selected?

assuming format 2 examples, i'd use split() , substring()

 string test = "myfile_test_india_20160728";  string countrycode = test.split("_")[2].substring(0,2);  system.out.println(countrycode); // print in  

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -