找出2个字符串的差异

时间:2017-02-08 10:39:36

标签: c# indexof

我在PowerPoint中有2个texbox。一个原件和一个编辑。我把它们串起来并将它们分成一个数组。然后我将它们与任何差异进行比较。例如,我的原始文本框有" This New"我的编辑有" Thisss Blaa"然后输出" This Thisss New Blaa"。但是当我的原始TextBox有两次相同的单词时,例如"这个"我的输出是#34; This Thiss Blaa"它应该在哪里" This Thiss This Blaa"。这只发生在字符串中有两个相同的单词时。有没有人有任何想法如何解决这个问题?

我的代码如下

            var q = from original in originalList
                    join editedTmp in editList on original.Id equals editedTmp.Id into g
                    from edited in g.DefaultIfEmpty()
                    select new
                    {
                        original,
                        edited
                    };
            foreach (var item in q)
            { 



            char delimiter = Convert.ToChar(" ");
            var originalString = item.original.TextFrame.TextRange.Text;
            var editString = item.edited.TextFrame.TextRange.Text;

            var firstStringList = originalString.Split(delimiter).ToList();

            var secondStringList = editString.Split(delimiter).ToList();


            foreach (var word in firstStringList)
            {
                if (secondStringList.IndexOf(word) == -1)
                {




                    var indexOfWord = firstStringList.IndexOf(word); //gets the position of the edited word eg. 3.
                                                                     // using indexOfWord+indexOfWord as I need do this if more than one word is added.
                    secondStringList.Insert(indexOfWord + indexOfWord, word);

                    // Insert the word that was not found at position 3 inside secondStringList

                    one.Add(word);


                    }
            }

1 个答案:

答案 0 :(得分:1)

所以,我想你想要:

字符串1:This This 字符串2:Thisss blaa

导致:This Thisss This Blaa

我实现这一目标的最简单方法是:

var first = "This This";
var second = "Thisss blaa";

var firstSplit = first.SplitBySpace();
var secondSplit = second.SplitBySpace();

var result = string.Join(" ", firstSplit.Zip(secondSplit, (firstString, secondString) => firstString + " " + secondString));
// result = "This Thisss This Blaa"

SplitBySpace就是这样:

public static class StringExtensions
{
    public static IEnumerable<string> SplitBySpace(this string value)
    {
        return value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    }
}

解释

.Zip接受两个数组并将每个元素连接在一起。相当于做:

for (var i = 0; i < arr1.Length; i++)
{
    var newElement = arr1[i] + arr2[i]
    arr3[i] = newElement;
}
// arr1.Length = 10, arr2.Length = 10;
// arr3.Length = 10!

但是,使用.Zip()的第二个参数,您可以定义如何创建新元素。

集成

var q = from original in originalList
        join editedTmp in editList on original.Id equals editedTmp.Id into g
        from edited in g.DefaultIfEmpty()
        select new
        {
            original,
            edited
        };

foreach (var item in q)
{ 
    var originalString = item.original.TextFrame.TextRange.Text;
    var editString = item.edited.TextFrame.TextRange.Text;
    var firstSplit = originalString .SplitBySpace();
    var secondSplit = editString .SplitBySpace();

    var result = string.Join(" ", firstSplit.Zip(secondSplit, (firstString, secondString) => firstString + " " + secondString));

    Console.WriteLine(result);
}