C# 两个字符串之间的差异

C# 两个字符串之间的差异,c#,string,array-difference,C#,String,Array Difference,我对二进制数进行运算,试图找出数组中只有一个差的两个字符串。比如说 string[]binary = {"{0000}","{0001}","{0111}"}; 第一个和第二个元素的第四个字符是不同的,我应该说明这一点,那么我如何才能找到它们呢?在C#中有什么有效的方法吗? 谢谢..这个怎么办,它会用一个不同的位查找所有二进制数对: class Program { static void Main( string[ ] args ) { string[] binary

我对二进制数进行运算,试图找出数组中只有一个差的两个字符串。比如说

string[]binary = {"{0000}","{0001}","{0111}"};
第一个和第二个元素的第四个字符是不同的,我应该说明这一点,那么我如何才能找到它们呢?在C#中有什么有效的方法吗?
谢谢..

这个怎么办,它会用一个不同的位查找所有二进制数对:

class Program {
    static void Main( string[ ] args ) {
        string[] binary = { "{0000}", "{0001}", "{0111}" };
        var differentNumbers = DifferentNumbers( binary );

        foreach ( Tuple<string, string, int> tuple in differentNumbers )
            Console.WriteLine( string.Format( "{0} ; {1} - index {2}", tuple.Item1, tuple.Item2, tuple.Item3 ) );
    }

    public static List<Tuple<string, string, int>> DifferentNumbers( string[ ] array ) {
        var differentNumbers = new List<Tuple<string, string, int>>( );

        for ( int i = 0; i < array.Length; i++ ) {
            for ( int j = i + 1; j < array.Length; j++ ) {

                int count = 0, index = 0;
                for ( int c = 1; c < array[ i ].Length - 1; c++ ) {
                    if ( array[ i ][ c ] != array[ j ][ c ] ) {
                        index = c;
                        if ( count++ > 1 )
                            break;
                    }
                }

                if ( count == 1 )
                    differentNumbers.Add( new Tuple<string, string, int>( array[ i ], array[ j ], index ) );
            }
        }

        return differentNumbers;
    }
}
类程序{
静态void Main(字符串[]参数){
字符串[]二进制={0000},{0001},{0111};
var differentNumbers=differentNumbers(二进制);
foreach(不同数量的元组)
WriteLine(string.Format(“{0};{1}-索引{2}”,tuple.Item1,tuple.Item2,tuple.Item3));
}
公共静态列表差异编号(字符串[]数组){
var differentNumbers=新列表();
for(int i=0;i1)
打破
}
}
如果(计数=1)
Add(新元组(数组[i],数组[j],索引));
}
}
返回不同的数字;
}
}
结果:

{0000};{0001}-索引4

这可能有助于:

public static int StringDistance(string first, string second)
{
    int result = 0;

    for (int i = 0; i < first.Length; i++)
    {
        if (!first[i].Equals(second[i]))
        {
            result++;
        }
    }

    return result;
}

对每个字符串迭代一次以转换为UInt64,然后可以非常有效地多次使用以相互测试:

    static private void TestCheckFor1BitDiff()
    {
        string[] values = new string[]
        {
            "0000",
            "0001",
            "0111",
            "1000",
            "1111"
        };

        UInt64[] intValues = values.Select(v => ConvertBinaryToUInt64(v)).ToArray();

        bool comp01 = CheckFor1BitDiff(intValues[0], intValues[1]); // true
        bool comp02 = CheckFor1BitDiff(intValues[0], intValues[2]); // false
        bool comp12 = CheckFor1BitDiff(intValues[1], intValues[2]); // false
        bool comp03 = CheckFor1BitDiff(intValues[0], intValues[3]); // true
        bool comp24 = CheckFor1BitDiff(intValues[2], intValues[4]); // true
    }

    private static UInt64 ConvertBinaryToUInt64(string value)
    {
        if (64 < value.Length)
        {
            throw new ArgumentException("exceeds 64 bits", "value");
        }

        UInt64 power = 1;
        UInt64 retVal = 0;

        foreach (char bit in value.ToArray().Reverse())
        {
            switch (bit)
            {
                case '1':
                    retVal += power;
                    break;
                case '0':
                    break;
                default:
                    throw new ArgumentException("contains characters other than 0 and 1", "value");
            }

            power *= 2;
        }

        return retVal;
    }

    static private bool CheckFor1BitDiff(string a, string b )
    {
        UInt64 a1 = ConvertBinaryToUInt64(a);
        UInt64 b1 = ConvertBinaryToUInt64(b);

        return CheckFor1BitDiff(a1, b1 );
    }

    static private bool CheckFor1BitDiff(UInt64 a, UInt64 b )
    {
        return IsPowerOfTwo( a ^ b );
    }

    static private bool IsPowerOfTwo(UInt64 value)
    {
        return (value & (value - 1)) == 0;
    }
静态私有void TestCheckFor1BitDiff()
{
字符串[]值=新字符串[]
{
"0000",
"0001",
"0111",
"1000",
"1111"
};
UInt64[]intValues=values.Select(v=>ConvertBinaryToUInt64(v)).ToArray();
bool comp01=CheckFor1BitDiff(intValues[0],intValues[1]);//真
bool comp02=CheckFor1BitDiff(intValues[0],intValues[2]);//false
bool comp12=CheckFor1BitDiff(intValues[1],intValues[2]);//false
bool comp03=CheckFor1BitDiff(intValues[0],intValues[3]);//真
bool comp24=CheckFor1BitDiff(intValues[2],intValues[4]);//真
}
专用静态UInt64 ConvertBinaryToUInt64(字符串值)
{
如果(64<值长度)
{
抛出新ArgumentException(“超过64位”,“值”);
}
UInt64功率=1;
UInt64 retVal=0;
foreach(value.ToArray().Reverse()中的字符位)
{
开关(位)
{
案例“1”:
retVal+=功率;
打破
案例“0”:
打破
违约:
抛出新ArgumentException(“包含0和1以外的字符”、“值”);
}
功率*=2;
}
返回返回;
}
静态私有bool CheckFor1BitDiff(字符串a、字符串b)
{
UInt64 a1=转换器二进制输入64(a);
UInt64 b1=ConvertBinaryToUInt64(b);
返回检查1BitDiff(a1、b1);
}
针对1BitDiff(UInt64 a、UInt64 b)的静态专用布尔检查
{
返回IsPowerOfTwo(a^b);
}
静态私有bool IsPowerOfTwo(UInt64值)
{
返回值(value&(value-1))==0;
}

您是只比较两个半字节还是全部三个半字节?您只想知道比较的半字节之间哪些位是不同的吗?您关心性能并将数字表示为字符串有什么特别的原因吗?而你想要的性能-O(n^2*m)(n个元素的数量,m个值的长度)是微不足道的…性能并不重要,我只是在数组@AlexeiLevenkovno的一个不同字符之间找到了双字符串,它是可以改变的,但我不在乎哪些位不同,我需要让这些字符串@moncadadit工作,我也明白这是什么,非常感谢。正确,但显然不是一种“有效”的方法来确定字符串在单个字符中是否不同。@AlexeiLevenkov你是对的。但是如果你读了评论,你会发现效率并不是这样。反正我做了编辑。@AlexeiLevenkov谢谢。我想知道是否有办法用LINQ编写
StringDistance
函数。有可能吗?Zip+Where+Count:
first.Zip(second,(f,s)=>new{f,s}).Where(i=>i.f!=i.s.).Count()
或Where:
first.Where((c,i)=>c!=second[i]).Count()
@AlexeiLevenkov非常感谢。
    static private void TestCheckFor1BitDiff()
    {
        string[] values = new string[]
        {
            "0000",
            "0001",
            "0111",
            "1000",
            "1111"
        };

        UInt64[] intValues = values.Select(v => ConvertBinaryToUInt64(v)).ToArray();

        bool comp01 = CheckFor1BitDiff(intValues[0], intValues[1]); // true
        bool comp02 = CheckFor1BitDiff(intValues[0], intValues[2]); // false
        bool comp12 = CheckFor1BitDiff(intValues[1], intValues[2]); // false
        bool comp03 = CheckFor1BitDiff(intValues[0], intValues[3]); // true
        bool comp24 = CheckFor1BitDiff(intValues[2], intValues[4]); // true
    }

    private static UInt64 ConvertBinaryToUInt64(string value)
    {
        if (64 < value.Length)
        {
            throw new ArgumentException("exceeds 64 bits", "value");
        }

        UInt64 power = 1;
        UInt64 retVal = 0;

        foreach (char bit in value.ToArray().Reverse())
        {
            switch (bit)
            {
                case '1':
                    retVal += power;
                    break;
                case '0':
                    break;
                default:
                    throw new ArgumentException("contains characters other than 0 and 1", "value");
            }

            power *= 2;
        }

        return retVal;
    }

    static private bool CheckFor1BitDiff(string a, string b )
    {
        UInt64 a1 = ConvertBinaryToUInt64(a);
        UInt64 b1 = ConvertBinaryToUInt64(b);

        return CheckFor1BitDiff(a1, b1 );
    }

    static private bool CheckFor1BitDiff(UInt64 a, UInt64 b )
    {
        return IsPowerOfTwo( a ^ b );
    }

    static private bool IsPowerOfTwo(UInt64 value)
    {
        return (value & (value - 1)) == 0;
    }