Jacoobyman Posted January 22, 2014 Share Posted January 22, 2014 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 :) Link to comment https://www.neowin.net/forum/topic/1197553-split-string-between-space-in-listbox/ Share on other sites More sharing options...
0 wrack Posted January 22, 2014 Share Posted January 22, 2014 What language? Link to comment https://www.neowin.net/forum/topic/1197553-split-string-between-space-in-listbox/#findComment-596227165 Share on other sites More sharing options...
0 Jacoobyman Posted January 22, 2014 Author Share Posted January 22, 2014 On 22/01/2014 at 02:01, wrack said: What language? Sorry Im an idiot lol. Visual Basic. Thanks. Link to comment https://www.neowin.net/forum/topic/1197553-split-string-between-space-in-listbox/#findComment-596227169 Share on other sites More sharing options...
0 snaphat (Myles Landwehr) Member Posted January 22, 2014 Member Share Posted January 22, 2014 http://msdn.microsoft.com/en-us/library/6x627e5f(v=vs.90).aspx ? then insert it however you want it into the listbox? Link to comment https://www.neowin.net/forum/topic/1197553-split-string-between-space-in-listbox/#findComment-596227171 Share on other sites More sharing options...
0 Andre S. Veteran Posted January 22, 2014 Veteran Share Posted January 22, 2014 Visual Basic 6 or Visual Basic 7+ (.NET)? In recent versions you just use "AAAAAA BBBBBB CCCCCCC".Split(' ') and that gives you an array containing ["AAAAAA", "BBBBBB", "CCCCCCC"]. Link to comment https://www.neowin.net/forum/topic/1197553-split-string-between-space-in-listbox/#findComment-596227309 Share on other sites More sharing options...
0 articuno1au Posted January 22, 2014 Share Posted January 22, 2014 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 :)) Link to comment https://www.neowin.net/forum/topic/1197553-split-string-between-space-in-listbox/#findComment-596227319 Share on other sites More sharing options...
0 jake1eye Posted January 22, 2014 Share Posted January 22, 2014 Here a simple VBS script that demo split. For Each i In Split("AAAA BBBB CCCC DDDD"," ") WScript.Echo i Next Link to comment https://www.neowin.net/forum/topic/1197553-split-string-between-space-in-listbox/#findComment-596228081 Share on other sites More sharing options...
0 firey Posted January 22, 2014 Share Posted January 22, 2014 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. Link to comment https://www.neowin.net/forum/topic/1197553-split-string-between-space-in-listbox/#findComment-596228331 Share on other sites More sharing options...
Question
Jacoobyman
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 :)
Link to comment
https://www.neowin.net/forum/topic/1197553-split-string-between-space-in-listbox/Share on other sites
7 answers to this question
Recommended Posts