如何比较两个字符串然后打印差异 [英] How to compare two string and then print the difference
本文介绍了如何比较两个字符串然后打印差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代表。 string s1 = jersey;
string s2 = jry;
使用asterisk打印j * r ** y $ / $
是否有任何算法比较两个字符串然后写差异。
btw我正在使用VS C#ASP.net web应用
谢谢
for ex. string s1=jersey;
string s2=jry;
print using asterisk j*r**y
is there any algorithm to compare two string and then write the diff.
btw I'm using VS C# ASP.net web application
thanks
推荐答案
使用LINQ你可以使用EXCEPT。
这里给出一个例子 Enumerable.Except(TSource)方法(IEnumerable(TSource)) ,IEnumerable(TSource))(System.Linq) [ ^ ]
从每个字符串(或2个char数组)中生成两个enumarables与exCEPT进行比较。
示例CONSOLE APP:
With LINQ you can use EXCEPT.
An example is given here Enumerable.Except(TSource) Method (IEnumerable(TSource), IEnumerable(TSource)) (System.Linq)[^]
Make two enumarables from each string (or 2 arrays of char) compare those with EXCEPT.
Example CONSOLE APP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DiffExcept
{
class Program
{
static void Main(string[] args)
{
string s1 = "jersey";
string s2 = "jry";
char[] c1 = s1.ToCharArray();
char[] c2 = s2.ToCharArray();
var diff = s1.Except(s2);
string newS1 = s1;
foreach(var value in diff)
{
newS1 = newS1.Replace(value, '*');
Console.WriteLine(value);
}
Console.WriteLine(newS1);
Console.Read();
}
}
}
我想以下代码嘻嘻你
I think below code hip you
string s1 = "jersey";
string s2 = "jry";
string final = string.Empty;
char[] aa = s1.ToCharArray();
foreach (char aaa in aa)
{
final = final + "*";
}
char[] bb = s2.ToCharArray();
int startindex=0;
foreach (char bbb in bb)
{
foreach (char aaa in aa)
{
if (s1.IndexOf(bbb, startindex) > -1)
{
final = final.Remove(s1.IndexOf(bbb, startindex), 1);
final = final.Insert(s1.IndexOf(bbb, startindex), bbb.ToString());
startindex = s1.IndexOf(bbb, startindex) + 1;
}
}
}
Response.Write(final);
这篇关于如何比较两个字符串然后打印差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文