c# - How to handle Dictionary Key case sensitivity -
all. trying code word replacement tool find , replace multiple words @ once. here's code:
idictionary<string, string> worddict = new dictionary<string, string>(); worddict[wordstofind.text] = wordstoreplace.text; string texttoreplace = article.text; foreach (keyvaluepair<string, string> entry in worddict) { texttoreplace = texttoreplace.replace(entry.key, entry.value); } worddict key,value array key word find , value word replace found word with. example, 1 entry in dictionary drone->dog.
wordstofind.text , wordstoreplace.text should self-explanatory. i'm demonstrating how worddict dictionary setup.
the article.text variable textblock has words needs replacing.
the problem running when word replacer hits capitalized words , doesn't recognize them word replace (case-sensitive).
without lowercasing , replacing way (or, i'll go lowercasing if have way retain original casing in output), trying find way detect capitalized words, first letter capitalized, , replace accordingly same casing replacement word when words in worddict variable lowercased.
you use regex.replace:
idictionary<string, string> worddict = new dictionary<string, string>(); worddict[wordstofind.text] = wordstoreplace.text; string texttoreplace = article.text; foreach (keyvaluepair<string, string> entry in worddict) { texttoreplace = regex.replace(texttoreplace, entry.key, entry.value, regexoptions.ignorecase); } but if original word example 'drone' (d uppercase) , replacement 'dog' d of dog still lowercase, need differently.
Comments
Post a Comment