'How to return number of digits match between 2 strings
I have 2 strings of same length. I need to know the number of digits the differ by.
string1: SH32OLP9843G, string2: SH12OLP9843G, Result: should be 1
string1: SH32OLP9843G, string2: SH12OLP0843G, Result: should be 2
string1: SH32OLP9843G, string2: SH12OLP08431, Result: should be 3
string1: SH32OLP9843G, string2: SH32OLP9843G, Result: should be 0
Im using this on SQL query like:
select * from sometable where fn_somefunction('string1',somefield) = 3
select * from sometable where someRegex between string1 and somefield = 1
Solution 1:[1]
Try following :
string[,] inputs = {{"SH32OLP9843G", "SH12OLP9843G"},
{"SH32OLP9843G", "SH12OLP0843G"},
{"SH32OLP9843G", "SH12OLP08431"},
{"SH32OLP9843G", "SH32OLP9843G"}};
for(int i = 0; i < inputs.GetLongLength(0); i ++)
{
char[] input1 = inputs[i, 0].ToCharArray();
char[] input2 = inputs[i, 1].ToCharArray();
int diff = input1.Select((x, index) => x == input2[index] ? 0 : 1).Sum();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jdweng |
