I've two strings StringA, StringB. I want to generate a unique string to denote this pair.
i.e.
f(x, y) should be unique for every x, y and f(x, y) = f(y, x) where x, y are strings.
Any ideas?
I've two strings StringA, StringB. I want to generate a unique string to denote this pair.
i.e.
f(x, y) should be unique for every x, y and f(x, y) = f(y, x) where x, y are strings.
Any ideas?
I think the following should yield unique strings:
(That is, there's only one place in the string where a single "@" sign can appear, and we don't have to worry about a run of "@"s at the end of StringA being confused with a run of "@"s at the start of StringB.
Just find a unique way of ordering them and concatenate with a separator.
For arbitrarily long lists of strings, either sort the list or generate a set, then concatenate with a separator:
Preferably, if the strings are long or the separator choice is a problem, use the hashes and hash the result:
You can use x.GetHashCode(). That not ensures that this will be unique, but quite. See more information in this question.
For example:
Just create a new class and override
Equals
&GetHashCode
:You could just sort them and concatenate them, along with, lets, say the lenght of the first word.
That way
f("one","two") = "onetwo3"
,f("two","one") = "onetwo3"
, and no other combination would produce that unique string as , e,g,"onet", "wo"
would yield"onetwo4"
However, this will be a abysmal solution for reasonably long strings.
You could also do some sort of hash code calculcation, like this
that would be reasonably unique, however, you can't guarantee uniqueness.
It would be nice if the OP provided a little more context, because this does not sound like a sound solution to any problem.
Compute a message digest of both strings and XOR the values
The message digest gives you unique value for each string and the XOR makes it possible for f(x, y) to be equal to f(y, x).
EDIT: As @Phil H observed, you have to treat the case in which you receive two equal strings as input, which would generate 0 after the XOR. You could return something like an
MD5(x+y)
if x and y are the same, andMD5(x) ^ MD5(y)
for the rest of values.