• 0

Split String between space in Listbox


Question

I am trying to split a single, long string by spaces into listbox ITEMS. For example:

I need AAAAA-AAAAA BBBBB-BBBBB CCCCCC-CCCCC

to be in listbox as:

AAAAA-AAAAA

BBBBB-BBBBB

CCCCC-CCCCC

 

 

 

In Visual Basic.

 

I know that there must be a simple solution and I have been unable to solve it. Help would be greatly appreciated.

Thank you :)

7 answers to this question

Recommended Posts

  • 0

String.Split is indeed the best way.

 

Then just iterate through your array to insert it into the list box. Foreach will make that easier :)

 

http://msdn.microsoft.com/en-us/library/5ebk1751.aspx <- Foreach usage. It's brilliant :D (just thought I'd add it in case you weren't familiar with the language :))

  • 0

in VB.NET (easily modified for C#) You would do something simple like..
 

        Dim str As String
        str = "AAA-AAA BBB-BBB CCC-CCC"

        ListBox1.Items.AddRange(str.Split(" "))

VB6 is close.. but a little different

 

        Dim str As String
        str = "AAA-AAA BBB-BBB CCC-CCC"
        For Each st In Split(str, " ")
            List1.AddItem (st)
        Next st

The way you do the thing in vb6 with the for each could be used in the .NET languages too, but you save a couple lines, and you are using a pre-existing function to do it.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.