[英]How do i find an incorrect words and their count in c#?
我喜歡從給定的文本中對不匹配/錯誤的單詞進行排序,例如字符串 s1="let's play Football" s2="let's paly fotbal and cricket"
現在我想要沒有。 String-s2 中的錯誤以及額外的單詞,如paly 和 fotbal、cricket以及類似的這些錯誤單詞的計數。
string s1 = "let's play football";
string s2 = "let's paly fotbal and cricket";
if (s1 != s2)
{
var str1Parts = s1.Split(' ');
var str2Parts = s2.Split(' ');
var wrongOrExtraWords = str2Parts.Where(s => !str1Parts.Contains(s)).ToList();
Console.WriteLine("Wrong words: ({0})", wrongOrExtraWords.Count());
foreach (var str2 in wrongOrExtraWords)
{
Console.WriteLine(str2);
}
}
else
{
Console.WriteLine("Both strings are equal.");
}
string s1 = "let's play football";
string s2 = "let's paly fotbal and cricket";
string[] sArr1 = s1.Split(' ');
string[] sArr2 = s2.Split(' ');
int wrongCount = 0;
for (int i=0; i< sArr1.Length; i++)
{
if (sArr1[i] != sArr2[i]) //for different words
wrongCount++;
}
wrongCount += sArr2.Length - sArr1.Length; //for extra words
//wrongCount = 4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.