我正在遍历字符串列表以查看该字符串是否包含在字典的值中,然后尝试从值中删除该字符串。
目前我这样做:
Dictionary<String, String> formValues = new Dictionary<String, String>();
formValues["key1"] = "the something at";
formValues["key2"] = "the something on";
formValues["key3"] = "the something is";
string prepositionList = "at,as,if,of,the,to,a,an,it,is,by,its";
List<string> prepositionListValues = new List<string>(prepositionList.Split(','));
foreach (string preposition in prepositionListValues)
{
List<string> keys = new List<string>(formValues.Keys);
foreach (string key in keys)
{
if (formValues[key] != null)
{
if (formValues[key].Contains(preposition))
{
formValues[key] = formValues[key].Replace(preposition, "");
}
}
}
}
对我来说,这似乎有点啰嗦。有没有“更好”的方式来做到这一点?