繁体   English   中英

根据有序的字符串对生成直观的唯一字符串?

[英]Generate an intuitive unique string based on an ordered pair of strings?

相关: 基于一对字符串生成唯一的字符串

我想生成一个直观的唯一字符串来表示有序的字符串对。

显然, stringA + stringB非常直观,但如果考虑例如"st" + "ring" == "stri" + "ng" == "string" ,则不是唯一的。

另外,与链接的OP不同,我想拥有uniqueString(stringA, stringB) != uniqueString(stringB, stringA) ,即非uniqueString(stringA, stringB) != uniqueString(stringB, stringA) 考虑到链接的OP,像MD5(stringA) - MD5(stringB)可能会起作用,但是我觉得这很不直观。

有任何想法吗?

如果遇到此类问题,我会尝试类似CSV的方法,例如

  • stringA + stringB => stringA;stringB

  • stringA + string;B => stringA;"string;B"

  • stringA + string"B => stringA;"string""B"

将第一个字符串的长度编码为结果字符串; 这样,您知道拆分的位置,并且“ xy” +“ z”与“ x” +“ yz”不同。
对长度进行零填充,以使其始终具有相同的位数(取决于字符串的最大长度)。

示例(最大字符串长度为999):

"xxx" + "yyy" = "003xxxyyy"  
"xx" + "xyyy" = "002xxxyyy"
"xxxyyy" + "" = "006xxxyyy"  
"" + "xxxyyy" = "000xxxyyy"  
"" + ""       = "000"

或者,如果字符串的最大长度未知,则可以在长度后使用定界符:

"xxx" + "yyy" = "3;xxxyyy"  

您不必为此使用特殊字符,也不必在字符串中使用定界符,因为没有歧义:

"a;b" + ";c;" = "3;a;b;c;" = length + delimiter + "a;b;c;"

这感觉非常像一个序列化问题……将两个值放在同一位置,之后仍然可以将它们分开。

最简单的方法之一是使用定界符àla csvs,尽管这将要求您实现唯一的字符或字符序列。

消除此问题就像在字符串中该定界符的所有实例以及所有'\\'之前添加一个'\\'一样简单。

举个例子:

"hello, " + "wor\d"
"hello\, " + "wor\\d" //Add in the escape characters
"hello\, ,wor\\d" //Second comma is not escaped, parser knows to split the string back into two components there

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM