String Manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kellysgirl
    New Member
    • Mar 2007
    • 9

    String Manipulation

    Now what you are going to see posted here is both the set of instructions I was given..and the code I have written.


    The instructions I was given are as follows

    In this case, you will create a Visual Basic 2005 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 SubString 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

    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.



    Step 6 – Finish up:

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



    The code I have written





    Option Strict On
    Option Explicit On


    Public Class MainForm
    Dim delimeter As String
    Dim oldIndex As Integer
    Dim newIndex As Integer
    Dim length As Integer
    Dim tempString As String
    Dim tempWord As String
    Dim advanceSize As Integer
    Dim i As Integer



    Private Sub exitButton_Clic k(ByVal sender As Object, ByVal e As System.EventArg s) Handles exitButton.Clic k
    Me.Close()

    End Sub

    Private Sub parseButton_Cli ck(ByVal sender As Object, ByVal e As System.EventArg s) Handles parseButton.Cli ck







    Do While oldIndex < TextBox1.Text.L ength
    newIndex = TextBox1.Text.I ndexOf(delimete r, oldIndex)
    If newIndex = -1 Then
    newIndex = Textbox1.text.l ength()

    Tempword=Textbo x1.text.substri ng(oldindex,_ NewINdex-Oldindex)
    ListBox1.items. add(tempWord)
    newIndex = newIndex + advanceSize
    oldIndex = newIndex


    End If



    Loop
    End Sub



    Private Sub buildButton_Cli ck(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles buildButton.Cli ck


    For i = 0 To (ListBox1.Items .Count - 1)
    TextBox1.Text = TextBox1.Text&L istbox1.Item(i)
    Next

    End Sub

    Private Sub clearButton_Cli ck(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles clearButton.Cli ck
    TextBox1.Clear( 0)
    End Sub
    End Class


    Where I am cofused is on this set of instruction

    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.


    as well as how to code getting the words into the list which are apples oranges bananas, cherries.


    can anyone out there clear up some of the confusion cuz I have no idea if Im going in the right direction with this program
  • pongscript
    New Member
    • Mar 2007
    • 27

    #2
    you could try split instead

    syntax:

    Dim MyList() as string = Mychardelimited string.Split(yo urdelimiter)

    just try


    then to add it to a listbox
    int i as long
    for i=lbound(MyList ) to Ubound(myList)
    listBox1.items. Add(Mylist(i))
    next

    Comment

    Working...