|
-
Feb 20th, 2014, 01:17 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Splitting string with multiple delimiters
I can't see where I am going wrong. I have a text file with this in it:
dept=div1,div2,div3
dept2=div1,div4
I tried this but where does the string after the = go?
Code:
Dim tfLines() As String = System.IO.File.ReadAllLines("text.ini") ' File to load.
For Each line As String In tfLines ' Load and read all lines in file.
DeptDropDown = line.Split("="c) ' Split using =.
DivDropDown = DeptDropDown(1).Split(","c) ' Split using ,.
Next
That errors out on the second split with : "Index was outside the bounds of the array."
My idea was to load dropdown1 and once changed load what is after the = sign to another drop down depending on what was selected in dropdown1. So if I selected "dept" in dropdown1 then dropdown2 will contain div1 div2 div3 in succession
-
Feb 20th, 2014, 02:10 PM
#2
Re: Splitting string with multiple delimiters
DivDropDown = line.split(new string(){"=", ","}, stringsplitoptions.none)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 20th, 2014, 02:14 PM
#3
Re: Splitting string with multiple delimiters
Actually, that wouldn't work. I'd recommend regex.split
Can you post a more specific example of how each line would be?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 20th, 2014, 02:19 PM
#4
Re: Splitting string with multiple delimiters
The best thing to do is to set up a breakpoint on both and see what strings are being returned, but in this particular case I'd do something like this:
Code:
Sub Main()
Dim txtfile() As String = {"dept=div1,div2,div3", "dept2=div1,div4"}
For Each line As String In txtfile
'Get the department by getting the substring from the beginning to the first instance of the equal sign
Dim dept As String = line.Substring(0, line.IndexOf("="c))
'Get all divs that are after the equal sign and seperated by commas
Dim divs() As String = line.Substring(line.IndexOf("="c) + 1).Split(","c)
'Write out what department we found
Console.WriteLine("Department: " & dept & " has the following DIV's")
For Each div As String In divs
'Write out each div found
Console.WriteLine(" " & div)
Next
'Write a seperating line to let the user know we started a new department
Console.WriteLine("------------------")
Next
Console.ReadLine()
End Sub
Edit - I see that .paul. beat me to the punch :P
-
Feb 20th, 2014, 02:45 PM
#5
Re: Splitting string with multiple delimiters
ok. i'm at my PC now...
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sedisplaynder As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = datasource(IO.File.ReadAllLines("C:\Users\Paul\Desktop\text.ini")).ToArray
End Sub
Private Iterator Function datasource(arr() As String) As IEnumerable(Of listItem)
For Each line As String In arr
If line.Trim = "" Then Continue For
Dim fields() As String = line.Split(New String() {"=", ","}, StringSplitOptions.None)
Yield New listItem With {.display = fields(0), .value = fields.Skip(1).ToArray}
Next
End Function
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox2.DataSource = DirectCast(ComboBox1.SelectedItem, listItem).value
End Sub
End Class
Public Class listItem
Public display As String
Public value() As String
Public Overrides Function ToString() As String
Return Me.display
End Function
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 20th, 2014, 04:10 PM
#6
Thread Starter
Frenzied Member
Re: Splitting string with multiple delimiters
Thank you guys. dday9, I couldn't quite get yours to do what I wanted, gave me errors on the first line.substring, but I believe it was more my code stopping it then anything else. But paul, thank you very much, I added it as a module because I will have a lot of forms using the same values in other dropdowns, no sense of using the same code over and over in different spots
-
Feb 20th, 2014, 05:00 PM
#7
Thread Starter
Frenzied Member
Re: Splitting string with multiple delimiters
small problem paul, when I save the form it errors out saying "Conversion from type "listitem" to type String is not valid"
Saving it like this
row("PODept") = Replace(CStr(CmboPODept.SelectedItem), "'", "`")
and it doesn't like that line
-
Feb 20th, 2014, 05:14 PM
#8
Re: Splitting string with multiple delimiters
change:
Code:
CStr(CmboPODept.SelectedItem)
to:
Code:
CmboPODept.GetItemText(CmboPODept.SelectedItem)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 20th, 2014, 05:20 PM
#9
Re: Splitting string with multiple delimiters
or just:
for the selecteditem...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 20th, 2014, 05:29 PM
#10
Thread Starter
Frenzied Member
Re: Splitting string with multiple delimiters
I can easily do it like this
row("PODept") = Replace(CmboPODept.SelectedItem.ToString, "'", "`")
but I know that it not the right way to go
[edit]Sorry, didn't refresh to see your post, that worked, thanks paul.
-
Feb 21st, 2014, 03:12 PM
#11
Thread Starter
Frenzied Member
Re: [RESOLVED] Splitting string with multiple delimiters
Hi paul,
One thing I noticed is after saving the item, I try to edit it, the dropdowns don't come back with what was saved. It loads like it is a brand new list.
I've tried both of these
CmbDept.Text = CStr(data("Department").ToString)
CmbDept.SelectedItem = CStr(data("Department").ToString)
doesn't effect the datasource of the combobox. I also tried it after the combo was filled.
-
Feb 21st, 2014, 03:20 PM
#12
Re: [RESOLVED] Splitting string with multiple delimiters
after the combo is filled:
Code:
CmbDept.SelectedIndex = CmbDept.FindStringExact(data("Department").ToString)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 21st, 2014, 03:55 PM
#13
Thread Starter
Frenzied Member
Re: [RESOLVED] Splitting string with multiple delimiters
funny, I was just reading about that method. It finds the correct index but doesn't show it selected in the box
-
Feb 21st, 2014, 03:58 PM
#14
Thread Starter
Frenzied Member
Re: [RESOLVED] Splitting string with multiple delimiters
nevermind paul, it was a PEBKAC lmao
-
Feb 21st, 2014, 04:13 PM
#15
Re: [RESOLVED] Splitting string with multiple delimiters
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 21st, 2014, 04:25 PM
#16
Re: [RESOLVED] Splitting string with multiple delimiters
 Originally Posted by .paul.
A what???
My thoughts exactly.
-
Feb 21st, 2014, 05:28 PM
#17
Thread Starter
Frenzied Member
Re: [RESOLVED] Splitting string with multiple delimiters
PEBKAC = Problem exist between keyboard and chair.
lol
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|