如何在asp.net中比较两个字符串之间变化的单词

本文关键字:字符串 两个 之间 变化 单词 asp net 比较 | 更新日期: 2023-09-27 18:03:50

在我的应用程序中,我有两个字符串,我需要显示缺失的计数单词,插入计数单词和在它们之间修改计数单词。例如:

  string variable1="When you are writing server code you can never be sure what the IP address you see is refereeing to.In fact some users like it this way.";
  string variable2="when are wrting server code yu cannn never be sure **Demo** what the address you is to.In fact **Demo1** some users like it this way";

结果应该是:

  Missing Words: you, see ,IP
  Missing Words count: 3
  Inserted: Demo, Demo1 
  Inserted Words count: 2
  Modified words : wrting,yu ,cannn ,refering
  Modified words count :4

我试过了,但在修改后的文字

中显示不正确
string variable1="When you are writing server code you can never be sure what the IP address you see is refereeing to.In fact some users like it this way.";
   string variable2="when are wrting server code yu cannn never be sure **Demo** what the IP address you see is to.In fact **Demo1** some users like it this way";
     //Missing Word Count    
     var result = variable1.Split(new char[] { ' ' }).Except(variable2.Split(new char[] { ' ' })).ToArray();
     count = result.Length;
     Label2.Text += "Missing Word Count: " + count.ToString() + "<br/><br/>";
     for (int i = 0; i < count; i++)
     {
       Label1.Text += "Missing Word: " + result[i].ToString() + "<br/><br/>";
     }

     //Insert Word
     var result1 = variable2.Split(new char[] { ' ' }).Except(variable1.Split(new char[] { ' ' })).ToArray();
     count = 0;
     count = result1.Length;
     for (int i = 0; i < count; i++)
     {
         Label3.Text += "Insert Word: " + result1[i].ToString() + "<br/><br/>";
     }
     Label4.Text += "Insert Word Count: " + count.ToString() + "<br/><br/>";    
     //Modifide Words
     string[] tempArr1 = variable1.Split(' ');  
     string[] tempArr2 = variable2.Split(' ');  
     int counter = 0;  
     for (int i = 0; i < tempArr1.Length; i++)  
     {  
         if (tempArr1[i] != tempArr2[i])  
         {  
            lblwords.text=tempArr1[i] + ", "+ tempArr2[i];  
             counter++;  
         }  
     } 
谁能告诉我怎么做这件事?

谢谢

如何在asp.net中比较两个字符串之间变化的单词

您可以使用Linq来实现这个

string s1 = "When you are writing server code you can never be sure what the IP address you see is refereeing to.In fact some users like it this way.";
string s2 = "are wrting server code yu cannn never be sure what the IP address you see is to.In fact some users like it this way.";
var list = s2.Split(' ').Where(x => (!s1.Split(' ').Contains(x))).ToList();
int count = list.Count;
foreach (var item in list)
{
 //your code
}