vb.net - 在一个字符串中创建两个字符串

时间:2014-09-14 06:48:20

标签: vb.net duplicates

例如:

Dim str1 = "{goat}{ball}{troll}"
Dim str2 = "{apple}{goat}{troll}"

然后我想删除重复项并看到类似的内容:

Dim str3 = "{goat}{ball}{troll}{apple}"

你能帮我吗?

1 个答案:

答案 0 :(得分:1)

Dim Delimiters As Char() = "}{".ToCharArray ' Or: {"}"c, "{"c}

Dim str1 As String = "{goat}{ball}{troll}"
Dim str2 As String = "{apple}{goat}{troll}"
Dim str3 As String = String.Empty

Dim UniqueItems As List(Of String) =
    (str1 & str2).Split(Delimiters, StringSplitOptions.RemoveEmptyEntries).
                  Distinct.ToList

str3 = String.Format("{{{0}}}", String.Join(CStr(Delimiters), UniqueItems))

结果字符串:

{goat}{ball}{troll}{apple}