两个字符串/集的交集

时间:2012-10-22 18:52:20

标签: delphi char set intersection delphi-5

来自python我在delphi5中寻找与此python代码(sets)相当的东西:

>>> x = set("Hello")
>>> x
set(['H', 'e', 'l', 'o'])

>>> y = set("Hallo")
>>> y
set(['a', 'H', 'l', 'o'])

>>> x.intersection(y)
set(['H', 'l', 'o'])

1 个答案:

答案 0 :(得分:8)

var
  a, b, c: set of byte;
begin
  a := [1, 2, 3, 4];
  b := [3, 4, 5, 6];
  c := a*b;          // c is the intersection of a and b, i.e., c = [3, 4]

但要注意:

var
  a, b, c: set of integer;

甚至不会编译;相反,你得到'集合最多可能有256个元素'错误。有关Delphi集的更多信息,请参阅documentation

<强>更新

抱歉,忘了提及'明显'(从Delphi程序员的角度来看):

var
  a, b, c: set of char;
begin
  a := ['A', 'B', 'C', 'D'];
  b := ['C', 'D', 'E', 'F'];
  c := a*b;          // c is the intersection of a and b, i.e., c = ['C', 'D']

但你的字符都是字节字符 - 也就是说,忘掉Unicode(Delphi 5不支持Unicode,所以在这种情况下这不是一个限制)!