0

I have a set of strings:

121010

121010

121011

121011

What I want to do is take all of the values and merge them together so I can get an output like this:

121010

121011

For instance, a listbox should search through that string and then give each result as a list item, if there was 400 "121010" values it would display as one value.

I have looked every where for a solution but I am now totally lost.

Any sort of reference would be appreciated.

Dylan de St Pern
  • 469
  • 1
  • 7
  • 20
  • Can you add some code that you have tried so far? – ekad Feb 12 '14 at 08:53
  • Can you post sample input? your samples doesn't seems to be good enough to understand – Linga Feb 12 '14 at 08:53
  • So you have a single string or do you have a collection of strings (like a `List(Of String)`? If you have a collection, look into the `Distinct` extension method, or use a `HashSet` as collection. – sloth Feb 12 '14 at 08:54
  • Sorry, my mistake. each line is a separate string. I will look into it, Thanks. – Dylan de St Pern Feb 12 '14 at 08:56

3 Answers3

2

You can use the Distinct-extension-method:

Dim lst As New List(Of String)()
lst.Add("121010")
lst.Add("121011")
lst.Add("121010")
lst.Add("121011")
Dim distinctEntries = lst.Distinct()

As you are comparing strings, you might want to consider an overload that also takes a comparer as input (in order to handle the casing of the letters in the way you want):

Dim distinctEntries = lst.Distinct(StringComparer.OrdinalIgnoreCase)
Markus
  • 20,838
  • 4
  • 31
  • 55
0

Your question suppose to be how to display unique list

Before adding to listbox,

  • check whether item is added already
  • If not, add it.

This is a basic coding style of check.

In the advance style of coding, you can use Distinct()

Siva Charan
  • 17,940
  • 9
  • 60
  • 95
0

You can use HashSet collection as below:

''String array.
    Dim a As String() = {"121010", "121010", "121011", "121011"}

    ' Create HashSet.
    Dim hash As HashSet(Of String) = New HashSet(Of String)(a)

    ' String array.
    a = hash.ToArray()

HashSet VS Distinct.

Community
  • 1
  • 1
binarymnl
  • 153
  • 2
  • 12