Seperating text in a textbox?
Let's say I paste this text in textbox1
"firstname" : john, "lastname" : smith, "country" : usa, "firstname" : billy, "lastname" : brown, "country" : canada, "firstname" : richard, "lastname" : wong, "country" : usa
How could I split up each "firstname, lastname, and country" up? And like add them to 3 listboxes?
I'm pretty much self taught visual basic, using a lot of forums and all, andwas wondering if this is possible. I normally google too but i really have no idea what query I would search (the term). Atm, I'm having troubles making a find replace replaceall thing, so I really can't see myself making something like find '"firstname" : ' and get the text after that till the coma.
So if someone could direct me to somewhere, explain how it works, and/or give an example it'd be great :)
Re: Seperating text in a textbox?
1. Call Split on the string and specify a comma as the character to split on. This will give you a String array.
2. Create three For loops, each with a Step of 3. The first loop starts at 0, the second at 1 and the third at 2.
3. Inside the first loop, get the element at the current index and split it on the colon to get a String array.
4. Get the second element of the array and call Trim on it, then add it to a List of first names.
5. Repeat steps 3 & 4 for the other two loops and Lists of last names and countries.
6. Bind the three Lists to the three ListBoxes.
Re: Seperating text in a textbox?
If the format never changes you could possibly do it within a single loop.
Code:
Dim sTmp() As String = TextBox1.Text.Split(",")
For i As Integer = 0 To sTmp.Length - 1 Step 3
ListBox1.Items.Add(Trim(Mid(sTmp(i), InStr(1, sTmp(i), ":") + 1)))
ListBox2.Items.Add(Trim(Mid(sTmp(i + 1), InStr(1, sTmp(i + 1), ":") + 1)))
ListBox3.Items.Add(Trim(Mid(sTmp(i + 2), InStr(1, sTmp(i + 2), ":") + 1)))
Next i
Re: Seperating text in a textbox?
Ok thanks :) I'll try that
Re: Seperating text in a textbox?
Quote:
Originally Posted by
Raeki
Ok thanks :) I'll try that
The above requires the Microsoft.VisualBasic ref., without it this is what I came up with so far, probably needs some tweaking. ;)
Code:
Dim sTmp() As String = TextBox1.Text.Split(",")
For i As Integer = 0 To sTmp.Length - 1 Step 3
ListBox1.Items.Add(sTmp(i).Substring(sTmp(i).IndexOf(":") + 2, (sTmp(i).Length - 1) - (sTmp(i).IndexOf(":") + 1)))
ListBox2.Items.Add(sTmp(i + 1).Substring(sTmp(i + 1).IndexOf(":") + 2, (sTmp(i + 1).Length - 1) - (sTmp(i + 1).IndexOf(":") + 1)))
ListBox3.Items.Add(sTmp(i + 2).Substring(sTmp(i + 2).IndexOf(":") + 2, (sTmp(i + 2).Length - 1) - (sTmp(i + 2).IndexOf(":") + 1)))
Next i
Re: Seperating text in a textbox?
One problem with populating each ListBox separately, however you do it, is that there will be no specific association between the items in the three controls. If it's your intention that selecting an item in one list would select the corresponding items in the other two then you'd have to implement that manually.
A better way would be to define a class that encapsulates a set of data, i.e. has properties for each of the values. You can then create a single list of instances of that type and bind it to all three controls. The association between items will then be maintained automatically.
vb.net Code:
Friend Class Record
Public Property FirstName As String
Public Property LastName As String
Public Property Country As String
End Class
vb.net Code:
Dim parts As String() = myString.Split(","c)
Dim values As String() = Array.ConvertAll(parts, Function(s) s.Split(":"c)(1).Trim())
Dim records As New List(Of Record)
For index = 0 To values.GetUpperBound(0) Step 3
records.Add(New Record With {.FirstName = values(index),
.LastName = values(index + 1),
.Country = values(index + 2)})
Next
Me.firstNameListBox.DisplayMember = "FirstName"
Me.firstNameListBox.DataSource = records
Me.lastNameListBox.DisplayMember = "LastName"
Me.lastNameListBox.DataSource = records
Me.countryListBox.DisplayMember = "Country"
Me.countryListBox.DataSource = records
Re: Seperating text in a textbox?
Ok I'll try that now - yah was having troubles trying to get a way to link them together. ty again :)
1 Attachment(s)
Re: Seperating text in a textbox?
Hmmm where do I insert that code? Whether I post the friend class inside or ouside "public class form 1" I get errors (shown in attachement)
sorry! i really don't know anything lol
Re: Seperating text in a textbox?
This is why you should use the Prefix drop-down to specify your version when creating a new thread. If you don't I will assume that you're using the most recent version and post code accordingly. Those are auto-implemented properties and are only supported in VB 2010. If you're using an older version then you will need to declare conventional properties with backing fields.
Also, that is a class like any other class. You wouldn't declare it inside the form, which is itself a class. Just as you would do for any other class, you add a new code file. Use the Solution Explorer or Project menu to add a new class just as you would to add a new form, then add the appropriate properties.
Re: Seperating text in a textbox?
Oh okay sorry I didn't know. I'm using Visual Basic 2008 but I'll fiddle around with it (i have 2010 too)
1 Attachment(s)
Re: Seperating text in a textbox?
Still have an error starting a brand new class (not sure how to explain posted in pics)
vb Code:
Public Property FirstName() As String
Get
End Get
Set(ByVal value As String)
End Set
End Property
Public Property LastName() As String
Get
End Get
Set(ByVal value As String)
End Set
End Property
Public Property Country() As String
Get
End Get
Set(ByVal value As String)
End Set
End Property
Managed to get here - do I have to do anything with this?
Re: Seperating text in a textbox?
The location of the class was not the point of my post.
Quote:
Those are auto-implemented properties and are only supported in VB 2010. If you're using an older version then you will need to declare conventional properties with backing fields.
Any beginners tutorial will cover declaring properties, e.g.
http://www.homeandlearn.co.uk/net/nets11p5.html
Re: Seperating text in a textbox?
Hmmm I'm having some troubles. Whatever I put in the "get" seems to be whatever populates the listboxes
Re: Seperating text in a textbox?
Btw, I've been able to make it into just
john,1234567890,canada,bc,male,mark,2345678901,usa,washington,male etc.etc.,
I'll try to get the coding for it but if someone could give an example it'd be great :)
I got this code
vb Code:
Dim myarray() As String = TextBox1.Text.Split(",")
For a As Integer = 0 To myarray.Length - 1 Step 6
ListBox1.Items.Add(myarray(a).Substring(a).IndexOf(","))
ListBox2.Items.Add(myarray(a + 1).Substring(a + 1).IndexOf(","))
ListBox3.Items.Add(myarray(a + 2).Substring(a + 2).IndexOf(","))
ListBox4.Items.Add(myarray(a + 3).Substring(a + 3).IndexOf(","))
ListBox5.Items.Add(myarray(a + 4).Substring(a + 4).IndexOf(","))
ListBox6.Items.Add(myarray(a + 5).Substring(a + 5).IndexOf(","))
Next
And it keeps telling me "start index is greater than the length of the string"?
And with this code, I get "start index less than 0"
vb Code:
For i As Integer = 0 To sTmp.Length - 1 Step 6
ListBox1.Items.Add(sTmp(i).Substring(sTmp(i).IndexOf(","), (sTmp(i).Length - 1) - (sTmp(i).IndexOf(",") + 1)))
ListBox2.Items.Add(sTmp(i + 1).Substring(sTmp(i + 1).IndexOf(","), (sTmp(i + 1).Length - 1) - (sTmp(i + 1).IndexOf(",") + 1)))
ListBox3.Items.Add(sTmp(i + 2).Substring(sTmp(i + 2).IndexOf(","), (sTmp(i + 2).Length - 1) - (sTmp(i + 2).IndexOf(",") + 1)))
ListBox3.Items.Add(sTmp(i + 3).Substring(sTmp(i + 3).IndexOf(","), (sTmp(i + 3).Length - 1) - (sTmp(i + 3).IndexOf(",") + 1)))
ListBox3.Items.Add(sTmp(i + 4).Substring(sTmp(i + 4).IndexOf(","), (sTmp(i + 4).Length - 1) - (sTmp(i + 4).IndexOf(",") + 1)))
ListBox3.Items.Add(sTmp(i + 5).Substring(sTmp(i + 5).IndexOf(","), (sTmp(i + 5).Length - 1) - (sTmp(i + 5).IndexOf(",") + 1)))
Next i
Re: Seperating text in a textbox?
With your properties problem, here's what your new class should look like:
Code:
Friend Class PeopleClass
Private xFirst as String = ""
Private xLast as String = ""
Private xCountry As String = ""
Public Property FirstName() As String
Get
Return xFirst
End Get
Set (ByVal value As String)
xFirst = value
End Set
End Property
Public Property LastName() As String
Get
Return xLast
End Get
Set (ByVal value As String)
xLast = value
End Set
End Property
Public Property Country() As String
Get
Return xCountry
End Get
Set (ByVal value As String)
xCountry = value
End Set
End Property
End Class
Now that you have that class, you can define a list(Of Class), like so:
Code:
Private ListPeople As New List(Of PeopleClass)
Then add to the class like this (probably not the best way)
Code:
Dim firstArr() As String = TextBox1.Text.Split(","c)
For i As Integer = 0 to firstArr.Count -1 Step 3
Dim nClass As New PeopleClass
With nClass
.FirstName = firstArr(i).Split(":"c)(1).Trim
.LastName = firstArr(i+1).Split(":"c)(1).Trim
.Country = firstArr(i+2).Split(":"c)(1).Trim
End With
ListPeople.Add(nClass)
Next
And you can now simply assign the list as a datasource to a listbox and display a specific property or whatever.
Re: Seperating text in a textbox?
Quote:
Originally Posted by
J-Deezy
With your properties problem, here's what your new class should look like:
Code:
Friend Class PeopleClass
Private xFirst as String = ""
Private xLast as String = ""
Private xCountry As String = ""
Public Property FirstName() As String
Get
Return xFirst
End Get
Set (ByVal value As String)
xFirst = value
End Set
End Property
Public Property LastName() As String
Get
Return xLast
End Get
Set (ByVal value As String)
xLast = value
End Set
End Property
Public Property Country() As String
Get
Return xCountry
End Get
Set (ByVal value As String)
xCountry = value
End Set
End Property
End Class
Now that you have that class, you can define a list(Of Class), like so:
Code:
Private ListPeople As New List(Of PeopleClass)
Then add to the class like this (probably not the best way)
Code:
Dim firstArr() As String = TextBox1.Text.Split(","c)
For i As Integer = 0 to firstArr.Count -1 Step 3
Dim nClass As New PeopleClass
With nClass
.FirstName = firstArr(i).Split(":"c)(1).Trim
.LastName = firstArr(i+1).Split(":"c)(1).Trim
.Country = firstArr(i+2).Split(":"c)(1).Trim
End With
ListPeople.Add(nClass)
Next
And you can now simply assign the list as a datasource to a listbox and display a specific property or whatever.
Thanks :) The class code worked, and makes sense, but with the listboxing code it seems to split up the name and put it in each listbox, and I don't know what happens to the rest of the info. Trying something else right now though :)
btw what is the c for in (":"c)? i don't seem to find anything with it
Re: Seperating text in a textbox?
Lol, finally made a code, which was like half as long as the others.
Now I just have one problem; I have inserted them seperatly into listboxes first. Can I bind them after I've inserted them without relation into the listboxes? I found some examples but they were for older versions (i'm 2008)
Re: Seperating text in a textbox?
Quote:
Originally Posted by
Raeki
Thanks :) The class code worked, and makes sense, but with the listboxing code it seems to split up the name and put it in each listbox, and I don't know what happens to the rest of the info. Trying something else right now though :)
btw what is the c for in (":"c)? i don't seem to find anything with it
The c in (":"c) is to implicitly declare ":" as a char. Try turning on Option Strict and then using only (":"), then try with (":"c)
As for datasourcing the code, why not do this:
Code:
lstFirstNames.DisplayMember = "FirstName"
lstFirstNames.DataSource = ListPeople
lstLastNames.DisplayMember = "LastName"
lstLastNames.DataSource = ListPeople
lstCountry.DisplayMember = "Country"
lstCountry.DataSource = ListPeople
?
Re: Seperating text in a textbox?
Quote:
Originally Posted by
J-Deezy
The c in (":"c) is to implicitly declare ":" as a char. Try turning on Option Strict and then using only (":"), then try with (":"c)
That would be "explicitly".
Quote:
Originally Posted by
J-Deezy
As for datasourcing the code, why not do this:
Code:
lstFirstNames.DisplayMember = "FirstName"
lstFirstNames.DataSource = ListPeople
lstLastNames.DisplayMember = "LastName"
lstLastNames.DataSource = ListPeople
lstCountry.DisplayMember = "Country"
lstCountry.DataSource = ListPeople
?
Exactly as I said in post #6.
Re: Seperating text in a textbox?
I tried that earlier today and got a ton of extra spaces; although it may have been a mistake so I'll try again :)
Re: Seperating text in a textbox?
For some strange reason, with that code it only loads like 1/6th of the items there are. Btw, I'm testing with datagrids. Is there any way to simply add the listbox items into the datagrid columns? I did some google they all seemed to require some databinding but I was wondering if there's any way to add items as you would to a listbox
Re: Seperating text in a textbox?
Quote:
Originally Posted by
jmcilhinney
That would be "explicitly".Exactly as I said in post #6.
Ahh thanks for the correction, I always confuse myself what's implicit and what's explicit ;)
I know you said it in post 6, but I just thought I'd reiterate as it didn't seem like he'd done that.
EDIT, created an app to test my code and see what could be your problem...except I didn't get any problems. Here's my FULL code. (I used a button to split the textbox and add to the list(of class), then assign datasources. Here you go:
vb Code:
Public Class Form1
Private ListPeople As New List(Of PeopleClass)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim firstArr() As String = TextBox1.Text.Split(","c)
For i As Integer = 0 To firstArr.Count - 1 Step 3
Dim nClass As New PeopleClass
With nClass
.FirstName = firstArr(i).Split(":"c)(1).Trim
.LastName = firstArr(i + 1).Split(":"c)(1).Trim
.Country = firstArr(i + 2).Split(":"c)(1).Trim
End With
ListPeople.Add(nClass)
Next
ListBox1.DisplayMember = "FirstName"
ListBox1.DataSource = ListPeople
ListBox2.DisplayMember = "LastName"
ListBox2.DataSource = ListPeople
ListBox3.DisplayMember = "Country"
ListBox3.DataSource = ListPeople
End Sub
End Class
Friend Class PeopleClass
Private xFirst As String = ""
Private xLast As String = ""
Private xCountry As String = ""
Public Property FirstName() As String
Get
Return xFirst
End Get
Set(ByVal value As String)
xFirst = value
End Set
End Property
Public Property LastName() As String
Get
Return xLast
End Get
Set(ByVal value As String)
xLast = value
End Set
End Property
Public Property Country() As String
Get
Return xCountry
End Get
Set(ByVal value As String)
xCountry = value
End Set
End Property
End Class