• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4329
  • Last Modified:

IndexOf and Substring in VB.Net

on this code below regarding Form1_Load, i would like someone to explain to me this part of it:

----------------------------------------
sCurrentBook = BookStreamReader.ReadLine
            iPosition1 = sCurrentBook.IndexOf("|") + 1
            iPosition2 = sCurrentBook.IndexOf("|", iPosition1) + 1
            Book.Code = sCurrentBook.Substring(0, iPosition1 - 1)
            Book.Title = sCurrentBook.Substring(iPosition1, iPosition2 - iPosition1 - 1)
            Book.Price = sCurrentBook.Substring(iPosition2)
Note: book is a variable of Structure and the structure is not shown in this code.

-----------------------------------------



Private Sub Form1_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
        Dim BookStreamReader As StreamReader
        Dim sCurrentBook As String
        Dim iPosition1, iPosition2 As Integer
        BookFileStream = New FileStream(sPath, FileMode.Open, FileAccess.Read)
        BookStreamReader = New StreamReader(BookFileStream)
        Do Until BookStreamReader.Peek = -1
            sCurrentBook = BookStreamReader.ReadLine
            iPosition1 = sCurrentBook.IndexOf("|") + 1
            iPosition2 = sCurrentBook.IndexOf("|", iPosition1) + 1
            Book.Code = sCurrentBook.Substring(0, iPosition1 - 1)
            Book.Title = sCurrentBook.Substring(iPosition1, iPosition2 - iPosition1 - 1)
            Book.Price = sCurrentBook.Substring(iPosition2)
            Books.Add(Book)
            cboBooks.Items.Add(Book.Title)
        Loop
        BookStreamReader.Close()
        btnUpdate.Enabled = False
        btnCancel.Enabled = False
    End Sub
0
jskfan
Asked:
jskfan
1 Solution
 
lostcarparkCommented:
IndexOf finds the position of a specified substring within a string. If you specify a second parameter, it's the point withing the string to start looking from.

Substring returns a segment of a string, starting from the specified character and continuing for the nuimber of characters specified by the second parameter.

So, line by line:
            sCurrentBook = BookStreamReader.ReadLine
Read a line from the stream.
            iPosition1 = sCurrentBook.IndexOf("|") + 1
Find the position of the first | character..
            iPosition2 = sCurrentBook.IndexOf("|", iPosition1) + 1
Find the second | character.
            Book.Code = sCurrentBook.Substring(0, iPosition1 - 1)
Get the portion of the string up to the first |.
            Book.Title = sCurrentBook.Substring(iPosition1, iPosition2 - iPosition1 - 1)
Get the piece of string between the two | characters.
            Book.Price = sCurrentBook.Substring(iPosition2)
Get everything after the second | character.

Personally I'd rewrite it something like the following:

            aCurrentBook = BookStreamReader.Split("|")
            Book.Code = aCurrentBook(0)
            Book.Title = sCurrentBook(1)
            Book.Price = sCurrentBook(2)

This uses the Split method, which returns an array containing eacgh element for each piece of the string divided by the specified seperater. Then all that remains is to take her down to operate.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now