c# - Fix a string from supplied data -
let's have these items in list full of strings:
- cash
- cheque
- postal order
- credit card
- bank transfer
- etc... etc... etc...
i found nice thing called "levenshteindistance". working point. not return correct string if type wrong.
i thinking of going regex side this.
basically want type, example, "chq" , should return "cheque".
i have code try not working correctly:
foreach (string entry in lsbsupplieddata.items) { entr = entry.trim().replace(" ", ""); regex = new regex("^[" + inputstring + "]+$", regexoptions.ignorecase); if (regex.ismatch(inputstring)) { proposal = entry; //break; } }
can please me right direction? have list items should suggest, @ max 20 items (not big, performance not big issue).
you can try this:
var words = new[] { "cash", "cheque" ... }; string search = "chq"; var results = words .where(x => x.tolower() .intersect(search.tolower().distinct()).count() >= search.length);
this work , ignore case sensitivity , order of letters ex if type cqh
still return cheque
, if don't want happen, requires more work.also can change where
first
or firstordefault
if want single result instead of matches.
update: here version doesn't ignore order of letters:
var result = words .firstordefault(x => x.tolower().where(search.contains).sequenceequal(search));
Comments
Post a Comment