作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个在单词中运行拼写检查的方法,但是我想要的是,如果单词在我的错误列表中已经存在,则不应将其添加为错误。下面是我的代码。
public void CheckSpelling()
{
int lineno = 0;
bool start = false;
foreach (string line in _contentList)
{
lineno++;
if (line.Contains("<text>"))
{
start = true;
}
if (start)
{
foreach (Match match in Regex.Matches(line, "<.*?>[^<]+</(.*?)>", RegexOptions.IgnoreCase))
{
List<string> customdiclist = new List<string>(File.ReadAllLines(Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["customdic"])));
string[] strArray = Regex.Replace(match.Value, "</?[^>]+>", string.Empty).Split(' ');
foreach (string word in strArray)
{
if ((word.Trim() != string.Empty) && ((word.Substring(0, 1) != word.Substring(0, 1).ToUpper()) && !_helper.CheckSpelling(Regex.Match(word, "[a-zA-Z]+").Value) && !customdiclist.Contains(word)))
{
ErrorModel errorModel = new ErrorModel()
{
LineNumber = lineno,
ErrorMessage = "Please Check Misspelled words",
Text = word
};
ErrorList.Add(errorModel);
}
}
}
}
}
}
class Helper
{
private static readonly string DictPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["dictionary"]);
private readonly Hunspell _splCheck = new Hunspell(DictPath + @"\nl_NL.aff", DictPath + @"\nl_NL.dic");
public bool CheckSpelling(string strWord)
{
if(!_splCheck.Spell(strWord.Trim()))
{
return false;
}
return true;
}
}
最佳答案
您可以使用LINQ来检查ErrorList
是否已包含具有ErrorModel
属性的Text
对象,该属性具有您要检查的word
的值。
使用FirstOrDefault
可以检查它是否返回null。如果在列表中找不到项目,则将返回null作为默认值,因为您的列表是类对象的集合(其默认值为null)。
如果FirstOrDefault
返回null,则word
尚未添加到ErrorList
中。
有关更多信息,请参见When to use .First and when to use .FirstOrDefault with LINQ?。
foreach (string word in strArray)
{
if ((word.Trim() != string.Empty) && ((word.Substring(0, 1) != word.Substring(0, 1).ToUpper()) && !_helper.CheckSpelling(Regex.Match(word, "[a-zA-Z]+").Value) && !customdiclist.Contains(word)))
{
if (ErrorList.FirstOrDefault(e => e.Text == word) == null)
{
ErrorModel errorModel = new ErrorModel()
{
LineNumber = lineno,
ErrorMessage = "Please Check Misspelled words",
Text = word
};
ErrorList.Add(errorModel);
}
}
}
关于c# - 如何删除errorList中已经存在的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33538113/
这似乎是一个新手问题,但事实就是如此。在 jQuery Validate 插件的 invalidHandler 中,我有以下内容。 invalidHandler: function(form, val
当我试图通过继承 ErrorList 并覆盖其 as_ul 方法来适应 Bootstrap 错误 html 类时,我遇到了一个奇怪的问题。 这是没有覆盖的形式:http://192.241.167.2
我是一名优秀的程序员,十分优秀!