c# - Searching Country by Country code -
i working on search method, called ajax, , updates webgrid in mvc4. search go through list of project objects, contains fields.
one of fields country. , right code checks if input string contains search string:
private bool stringstartwith(string input, string searchstring) { bool startwith = false; var inputlist = new list<string>(input.tolower().split(' ').distinct()); var searchlist = new list<string>(searchstring.tolower().split(' ')); var count = (from inp in inputlist sear in searchlist inp.startswith(sear) select inp).count(); if (count == searchlist.count) startwith = true; return startwith; }
but want able search country code. if write "dk", should tell equal denmark.
i hope can it. thanks.
//update!!
iturtev answer helped me make method work should. had update method shown here:
private bool inputstartwithsearch(string input, string searchstring) { if(searchstring[searchstring.length-1].equals(' ')) searchstring = searchstring.substring(0,searchstring.length-2); bool startwith = false; var inputlist = new list<string>(input.tolower().split(' ').distinct()); var searchlist = new list<string>(searchstring.tolower().split(' ')); if (searchstring.length == 2) { var countrycode = new regioninfo(searchstring.toupper()).englishname; if (inputlist.any(country => country.tolower().equals(countrycode.tolower()))) { return true; } } var count = (from inp in inputlist sear in searchlist inp.startswith(sear) select inp).count(); if (count == searchlist.count) startwith = true; return startwith; }
thanks lot.
may can use regioninfo:
// returns bulgaria new regioninfo("bg").englishname;
Comments
Post a Comment