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
Post a Comment