Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Chapter 6 – Case 2 Parsing Strings Figure 1 Instructions In this case, you will

ID: 3889056 • Letter: C

Question

Chapter 6 – Case 2

Parsing Strings

Figure 1

Instructions

In this case, you will create a Visual Basic solution that manipulates strings. It will parse a string containing a list of items within a text box and put the individual items into the list box. It will build the textbox string by putting the list box items together into a single string. Parsing is based on the selected delimiter.

Step 1: Create the Project:

Create a Visual Basic Project using the project name “StringParser”.

Step 2 – Design the Form:

Design the form as shown in Figure 1. You will need three group boxes, three radio buttons, four button controls, one text box, one list box, and two label controls.

Step 3 – Add code in the Form’s Load event to select the first radio button:

In the Form’s Load event, write code to select the Comma choice in the radio buttons group box.

Step 4 – Add code in the Parse Text to List button’s Click event to parse and load the list box:

You will need the variables shown in Table 1:

           

Variable name

Type

Use

delimiter

String

Holds the String character representing the chosen delimiter

oldIndex

Integer

Holds the starting position in the string for the search

newIndex

Integer

Holds the position where the delimiter was found in the string

length

Integer

Holds the length of the input string

tempString

String

Holds the input string from the text box

tempWord

String

Holds the extracted word from the string

advanceSize

Integer

Holds the number of characters to advance the pointer, to skip over the delimiter

Table 1

Initialize:

First, clear the list box in case there are items from a previous use.

Validate and set the delimiter:

Use an If/ElseIf/Else statement to validate that a delimiter has been selected. CR-LF means “carriage return – line feed”, which causes a new line to be started. You will use a built-in constant to represent this, called vbCRLF. Note that vbCRLF is two characters long, while the other delimiters are only 1 character long.

For the selected delimiter radio button, set the delimiter variable to the actual character and set the advanceSize for that delimiter, using the values in Table 2:

Selected delimiter

Delimiter character

Advance size

Comma

,

1

CR-FL

vbCRLF

2

Space

“ “

1

Table 2

Use the Exit Sub statement to leave the Click event if no radio button was selected.

Parse the text box contents:

Parsing a string to break out the words involves a loop and two pointer variables (oldIndex and newIndex). Both start at the beginning position, which is 0. oldIndex will always point to the current starting position for the scan (and extraction). newIndex should be set to the position of the next delimiter. Inside the loop, do these steps:

Scan the string from the starting position (oldIndex) until you find a delimiter. Set newIndex to the position of the delimiter.

Extract the word from the starting position (oldIndex) up to but not including the delimiter position (newIndex). Use tempWord to hold the extracted word.

Trim off spaces, and load the extracted word into the list box.

Move the starting position (oldIndex) forward past the extracted word and past the delimiter.

Hints:

Use a While loop. The condition to use will be whether oldIndex has reached the end of the input string. You can determine this by getting the length of the input string.

Use the IndexOf method to scan for the selected delimiter, and assign the results of the IndexOf method to newIndex. newIndex therefore points to the location of the next delimiter.

Use the Mid function to extract the word.

Remember that the delimiter size has been assigned to advanceSize.

Remember that there probably is no delimiter at the very end. So when the scan no longer finds a delimiter, you must check to see if oldIndex is at the end of the string. If not, you should extract the remaining characters from oldIndex forward.

Step 5 – Add code in the Build Text From List button’s Click event to load the text box:

This part is simpler! You will take the items in the list box, combine them with the delimiter, and put the final string into the text box. You will need the variables shown in Table 3:

Variable name

Type

Use

delimiter

String

Holds the String character representing the chosen delimiter

i

Integer

Loop counter

length

Integer

Holds the length of the input string

tempString

String

Holds the input string from the text box

advanceSize

Integer

Holds the number of characters to advance the pointer, to skip over the delimiter

Table 3

Initialize:

First, clear the text box of items from a previous use.

Validate and set the delimiter:

This code will be identical to the code used in the Parse Text to List button’s click event to validate the delimiter choice.

Load the text box from the list box:

You will need a loop to iterate through all of the items in the list box. For each item in the list, concatenate its value with the tempString variable. If it is not the last item in the list, concatenate the sleeted delimiter also. After all items have been concatenated into tempString, assign it to the text box.

Hints:

You can use a For loop here, because you know the number of items in the list (the Items.Count property provides the count of items in the list box).

You can test to see if the loop counter has reached the last item by comparing it with the Items.Count property to determine if you need to add a delimiter for this item.

Step 6 – Finish up:

Be sure to add the code for the Clear button and the Exit button.

Step 7: Save and run

Save all files, then start the application. Test the program using various delimiters. Be sure to enter the data in the textbox using the delimiter you have selected. Figure 2 shows a sample run using commas in the text box.

Figure 2

Then change the delimiter and use the second button to load the textbox from the list box. Figure 3 shows the screen after changing the delimiter to CR-LF, and then clicking the Build Text from List button.

Figure 3

Note: There is another way to parse a string, but you need to use an array. The String method Split will split a string into its component pieces and place the pieces into an array.

Variable name

Type

Use

delimiter

String

Holds the String character representing the chosen delimiter

oldIndex

Integer

Holds the starting position in the string for the search

newIndex

Integer

Holds the position where the delimiter was found in the string

length

Integer

Holds the length of the input string

tempString

String

Holds the input string from the text box

tempWord

String

Holds the extracted word from the string

advanceSize

Integer

Holds the number of characters to advance the pointer, to skip over the delimiter

Explanation / Answer

Please find below code:

1) Form1.vb

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' selects comma
rdbComma.Checked = True
End Sub

Private Sub btnParse_Click(sender As Object, e As EventArgs) Handles btnParse.Click
Dim delimiter As String ' Holds the String character representing the chosen delimiter
Dim oldIndex As Integer ' Holds the starting position in the string for the search
Dim newIndex As Integer ' Holds the position where the delimiter was found in the string
Dim length As Integer ' Holds the length of the input string
Dim tempString As String ' Holds the input string from the text box
Dim tempWord As String ' Holds the extracted word from the string
Dim advanceSize As Integer ' Holds the number of characters to advance the pointer, to skip over the delimiter

' clear the list box
lbContents.Items.Clear()

If rdbComma.Checked Then
delimiter = ","
advanceSize = 1
ElseIf rdbCRLF.Checked Then
delimiter = "vbCRLF"
advanceSize = 2
ElseIf rdbSpace.Checked Then
delimiter = " "
advanceSize = 1
Else
Exit Sub
End If

tempString = txtItems.Text

oldIndex = 0
newIndex = 0

While oldIndex < tempString.Length
newIndex = tempString.Substring(oldIndex).IndexOf(delimiter)
tempWord = tempString.Substring(oldIndex, newIndex).Trim()
lbContents.Items.Add(tempWord)
oldIndex = tempString.IndexOf(delimiter) + advanceSize
'tempString = tempString.Substring(oldIndex).Trim()
End While

End Sub
End Class

2) Form1.Designer.vb

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.GroupBox1 = New System.Windows.Forms.GroupBox()
Me.GroupBox2 = New System.Windows.Forms.GroupBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.txtItems = New System.Windows.Forms.TextBox()
Me.btnParse = New System.Windows.Forms.Button()
Me.rdbComma = New System.Windows.Forms.RadioButton()
Me.rdbCRLF = New System.Windows.Forms.RadioButton()
Me.rdbSpace = New System.Windows.Forms.RadioButton()
Me.GroupBox3 = New System.Windows.Forms.GroupBox()
Me.btnBuildText = New System.Windows.Forms.Button()
Me.Label2 = New System.Windows.Forms.Label()
Me.lbContents = New System.Windows.Forms.ListBox()
Me.btnClear = New System.Windows.Forms.Button()
Me.btnExit = New System.Windows.Forms.Button()
Me.GroupBox1.SuspendLayout()
Me.GroupBox2.SuspendLayout()
Me.GroupBox3.SuspendLayout()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.rdbSpace)
Me.GroupBox1.Controls.Add(Me.rdbCRLF)
Me.GroupBox1.Controls.Add(Me.rdbComma)
Me.GroupBox1.Location = New System.Drawing.Point(12, 12)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(80, 264)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Select a delimiter"
'
'GroupBox2
'
Me.GroupBox2.Controls.Add(Me.btnParse)
Me.GroupBox2.Controls.Add(Me.txtItems)
Me.GroupBox2.Controls.Add(Me.Label1)
Me.GroupBox2.Location = New System.Drawing.Point(98, 12)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(324, 129)
Me.GroupBox2.TabIndex = 1
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "Build a List from a Textbox"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(21, 26)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(223, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Enter items, separated by the chosen delimiter"
'
'txtItems
'
Me.txtItems.Location = New System.Drawing.Point(24, 45)
Me.txtItems.Multiline = True
Me.txtItems.Name = "txtItems"
Me.txtItems.Size = New System.Drawing.Size(136, 68)
Me.txtItems.TabIndex = 1
'
'btnParse
'
Me.btnParse.Location = New System.Drawing.Point(193, 80)
Me.btnParse.Name = "btnParse"
Me.btnParse.Size = New System.Drawing.Size(116, 33)
Me.btnParse.TabIndex = 2
Me.btnParse.Text = "Parse Text to List"
Me.btnParse.UseVisualStyleBackColor = True
'
'rdbComma
'
Me.rdbComma.AutoSize = True
Me.rdbComma.Location = New System.Drawing.Point(6, 30)
Me.rdbComma.Name = "rdbComma"
Me.rdbComma.Size = New System.Drawing.Size(60, 17)
Me.rdbComma.TabIndex = 0
Me.rdbComma.TabStop = True
Me.rdbComma.Text = "Comma"
Me.rdbComma.UseVisualStyleBackColor = True
'
'rdbCRLF
'
Me.rdbCRLF.AutoSize = True
Me.rdbCRLF.Location = New System.Drawing.Point(6, 53)
Me.rdbCRLF.Name = "rdbCRLF"
Me.rdbCRLF.Size = New System.Drawing.Size(55, 17)
Me.rdbCRLF.TabIndex = 1
Me.rdbCRLF.TabStop = True
Me.rdbCRLF.Text = "CR-LF"
Me.rdbCRLF.UseVisualStyleBackColor = True
'
'rdbSpace
'
Me.rdbSpace.AutoSize = True
Me.rdbSpace.Location = New System.Drawing.Point(6, 76)
Me.rdbSpace.Name = "rdbSpace"
Me.rdbSpace.Size = New System.Drawing.Size(56, 17)
Me.rdbSpace.TabIndex = 2
Me.rdbSpace.TabStop = True
Me.rdbSpace.Text = "Space"
Me.rdbSpace.UseVisualStyleBackColor = True
'
'GroupBox3
'
Me.GroupBox3.Controls.Add(Me.lbContents)
Me.GroupBox3.Controls.Add(Me.btnBuildText)
Me.GroupBox3.Controls.Add(Me.Label2)
Me.GroupBox3.Location = New System.Drawing.Point(98, 147)
Me.GroupBox3.Name = "GroupBox3"
Me.GroupBox3.Size = New System.Drawing.Size(324, 129)
Me.GroupBox3.TabIndex = 3
Me.GroupBox3.TabStop = False
Me.GroupBox3.Text = "Build a Textbox from a List"
'
'btnBuildText
'
Me.btnBuildText.Location = New System.Drawing.Point(193, 80)
Me.btnBuildText.Name = "btnBuildText"
Me.btnBuildText.Size = New System.Drawing.Size(116, 33)
Me.btnBuildText.TabIndex = 2
Me.btnBuildText.Text = "Build Text from List"
Me.btnBuildText.UseVisualStyleBackColor = True
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(21, 26)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(200, 13)
Me.Label2.TabIndex = 0
Me.Label2.Text = "Use this list to build the Textbox contents"
'
'lbContents
'
Me.lbContents.FormattingEnabled = True
Me.lbContents.Location = New System.Drawing.Point(24, 47)
Me.lbContents.Name = "lbContents"
Me.lbContents.Size = New System.Drawing.Size(136, 69)
Me.lbContents.TabIndex = 3
'
'btnClear
'
Me.btnClear.Location = New System.Drawing.Point(122, 282)
Me.btnClear.Name = "btnClear"
Me.btnClear.Size = New System.Drawing.Size(99, 32)
Me.btnClear.TabIndex = 4
Me.btnClear.Text = "Clear"
Me.btnClear.UseVisualStyleBackColor = True
'
'btnExit
'
Me.btnExit.Location = New System.Drawing.Point(291, 282)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing.Size(99, 32)
Me.btnExit.TabIndex = 5
Me.btnExit.Text = "Exit"
Me.btnExit.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(433, 331)
Me.Controls.Add(Me.btnExit)
Me.Controls.Add(Me.btnClear)
Me.Controls.Add(Me.GroupBox3)
Me.Controls.Add(Me.GroupBox2)
Me.Controls.Add(Me.GroupBox1)
Me.Name = "Form1"
Me.Text = "Parsing Strings"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox1.PerformLayout()
Me.GroupBox2.ResumeLayout(False)
Me.GroupBox2.PerformLayout()
Me.GroupBox3.ResumeLayout(False)
Me.GroupBox3.PerformLayout()
Me.ResumeLayout(False)

End Sub
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txtItems As System.Windows.Forms.TextBox
Friend WithEvents btnParse As System.Windows.Forms.Button
Friend WithEvents rdbComma As System.Windows.Forms.RadioButton
Friend WithEvents rdbCRLF As System.Windows.Forms.RadioButton
Friend WithEvents rdbSpace As System.Windows.Forms.RadioButton
Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
Friend WithEvents btnBuildText As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents lbContents As System.Windows.Forms.ListBox
Friend WithEvents btnClear As System.Windows.Forms.Button
Friend WithEvents btnExit As System.Windows.Forms.Button

End Class

LET ME KNOW IN CASE YOU NEED FURTHER HELP!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote