locked
Splitting strings into array of objects - HOW? RRS feed

  • Question

  • Hi, I am wondering if any one knows how I can split a string, brought in from a .txt file.

    How would I split the string, which looks like this:

    14,Teach Yourself Visual Basic 2005,James Foxall,SAMS,2006

    So, these are: bookID,bookTitle,Author,Publisher,PublishDate

    And I need to split the string up at the comma's ( , ), but not have them included when eventually displayed on on the string. But I need them split up to go into an array of 'Book' objects. For which I have set the class up with the variables, bookID,  Title etc... Along with code to set and retrieve the data.

    So I need the first line from the .txt file to split up to go into the first book objects in the array, and "tagged" with bookID if it is the ID, or bookTitle of it is the book title, and so on.

    Our tutor has gone through with it, and I really need to know ASAP. Please.

    Any help or guidance will be greatfull.

    Also, if you could help on joining, or concatinating (I think that the right term), the sstrings back together to be saved in the .txt file.

    Robbie

     

    Sunday, March 18, 2007 3:31 PM

Answers

  •  

    Hi,

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Create the string to split.

    Dim myString As String = "14,Teach Yourself Visual Basic 2005,James Foxall,SAMS,2006"

    'Set up an array to receive the splitted text.

    Dim splitted() As String

    'Define the split character.

    Dim myChar As Char = ","

    'SPLIT IT!!

    splitted = myString.Split(myChar)

    'Show the results.---------------->

    Dim index As Integer

    'Go to the last or UpperBound of the splitted array.

    ' Note that arrays start with the 1st index of zero, not one.

    For index = 0 To UBound(splitted)

    'Show each entry in turn in a message box.

    MsgBox("Item " & index.ToString & " is " & splitted(index))

    Next

    End Sub

     

    To join ( concantenate, joining as concantenating ) strings use the & symbol.

    string1 = "To join in "

    string2 = "marriage."

    joinedString = string1 & string2

    You can also say

    joinedString= "Joined in " & string2

    or  joinedString= "Joined in " & "marriage."

    or any combination.

     

    Regards,

    S_DS

     

    Sunday, March 18, 2007 4:27 PM

All replies

  •  

    Hi,

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Create the string to split.

    Dim myString As String = "14,Teach Yourself Visual Basic 2005,James Foxall,SAMS,2006"

    'Set up an array to receive the splitted text.

    Dim splitted() As String

    'Define the split character.

    Dim myChar As Char = ","

    'SPLIT IT!!

    splitted = myString.Split(myChar)

    'Show the results.---------------->

    Dim index As Integer

    'Go to the last or UpperBound of the splitted array.

    ' Note that arrays start with the 1st index of zero, not one.

    For index = 0 To UBound(splitted)

    'Show each entry in turn in a message box.

    MsgBox("Item " & index.ToString & " is " & splitted(index))

    Next

    End Sub

     

    To join ( concantenate, joining as concantenating ) strings use the & symbol.

    string1 = "To join in "

    string2 = "marriage."

    joinedString = string1 & string2

    You can also say

    joinedString= "Joined in " & string2

    or  joinedString= "Joined in " & "marriage."

    or any combination.

     

    Regards,

    S_DS

     

    Sunday, March 18, 2007 4:27 PM
  • Thank you S_DS, I will look at this and have an attempt. Then I just gotta put them into arrays of 'Book' objects I created.

    Ahhhh, aint programming a load of fun. lol.

    Rob

    Monday, March 19, 2007 4:40 PM