Parsing-Strings solved

35.00 $

Category:

Description

5/5 - (2 votes)

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:

  1. Scan the string from the starting position (oldIndex) until you find a delimiter. Set newIndex to the position of the delimiter.
  2. Extract the word from the starting position (oldIndex) up to but not including the delimiter position (newIndex). Use tempWord to hold the extracted word.
  3. Trim off spaces, and load the extracted word into the list box.
  4. Move the starting position (oldIndex) forward past the extracted word and past the delimiter.

 

Hints: 

  1. 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.
  2. 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.
  3. Use the Mid function to extract the word.
  4. Remember that the delimiter size has been assigned to advanceSize.
  5. 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: 

  1. 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).
  2. 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.

 

 

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.

  • Parsing-Strings.zip