字符串数组比较

.Net技术 码拜 8年前 (2016-05-15) 911次浏览
现有N个字符串数组,诸如下列
1100 0100 1101 0011 1100 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010
1100 0100 1101 0011 1010 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010
1100 0100 1101 0011 0110 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010
现在想比对这些字符串不同之处,并获取其(红字)索引值,怎么实现?用linq更好,
解决方案

2

两个串可以这样

            var s1 = "1100 0100 1101 0011 1100 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010";
            var s2 = "1100 0100 1101 0011 1010 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010";
            var query = from a in s1.ToArray().Select((x, index) => new { k = index, v = x })
                        from b in s2.ToArray().Select((x, index) => new { k = index, v = x })
                        where a.k == b.k && a.v != b.v
                        select a.k;
            Console.WriteLine(string.Join(",", query));

应该是用循环更好

8

来个循环的:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> array = new List<string>();
            //array.Add("1100 ……");
            if (array.Count < 1)
                return;
            int minLength = array.Min(m => m.Length);
            for (int idx = 0; idx < minLength; idx++)
            {
                char ch = array[0][idx];
                for (int i = 1; i < array.Count; i++)
                {
                    if (array[i][idx] != ch)
                        Console.WriteLine(idx);
                }
            }
        }
    }
}

10

嗯,写成一个完整的测试,除了上面的“一条语句”就能写好的算法,其它的都很平常

using System;
using System.Collections.Generic;
using System.Linq;
namespace Inhertied
{
    class IL_Method_Test
    {
        public static void Main()
        {
            var s1 = "1100 0100 1101 0011 1100 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010";
            var s2 = "1100 0100 1101 0011 1010 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010";
            var s3 = "1100 0100 1101 0011 0110 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010";
            var arr = new List<string> { s1, s2, s3 };
            var result = from i in Enumerable.Range(0, arr[0].Length)
                         where !判断字符完全一致(arr, i)
                         select i;
            foreach (var i in result)
            {
                Console.Write("第{0}个字符有差异:", i);
                foreach (var s in arr)
                    Console.Write(s[i]);
                Console.WriteLine();
            }
            Console.WriteLine(".....................按任意键结束");
            Console.ReadKey();
        }
        private static bool 判断字符完全一致(List<string> arr, int i)
        {
            var c = arr[0][i];
            for (var j = 1; j < arr.Count; j++)
                if (arr[j][i] != c)
                    return false;
            return true;
        }
    }
}

10

所以还是最原始的最好

        static void Main(string[] args)
        {
            string[] a =
            {
                "1100 0100 1101 0011 1100 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010",
                "1100 0100 1101 0011 1010 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010",
                "1100 0100 1101 0011 0110 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010",
            };
            for (var i = 0; i < a[0].Length; i++)
            {
                for (var j = 0; j < a.Length; j++)
                {
                    if (a[j][i] != a[0][i])
                    {
                        Console.WriteLine(i);
                        break;
                    }                   
                }
            }
 
        }

10

支持楼上观点。灵活、高效
本人从另一种角度写个,希望执行效率更高些

string s1 = "1100 0100 1101 0011 1100 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010";
string s2 = "1100 0100 1101 0011 1010 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010";
string s3 = "1100 0100 1101 0011 0110 0100 1000 0000 0000 0000 0010 0100 1100 0000 1000 0000 0101 0000 0000 0000 0000 0000 1000 0000 0000 1000 0001 1010";
List<string> myArr = new List<string> { s1, s2, s3 };
int ColSum = 0;
int Row = 0;
List<int> Result = new List<int>();
for (int Col=0; Col < myArr[0].Length; Col++)
{
    if(myArr[0][Col].ToString()==" ")
    {
        continue;
    }
    for (Row=0; Row < myArr.Count; Row++)
    {
        ColSum += Convert.ToInt32(myArr[Row][Col].ToString());
    }
    if(!(ColSum==Row || ColSum==0))
    {
        Result.Add(Col);
    }
    ColSum = 0;
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明字符串数组比较
喜欢 (0)
[1034331897@qq.com]
分享 (0)