473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()

End Sub

Private Sub parseButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles parseButton.Click







Do While oldIndex < TextBox1.Text.Length
newIndex = TextBox1.Text.IndexOf(delimeter, oldIndex)
If newIndex = -1 Then
newIndex = Textbox1.text.length()

Tempword=Textbox1.text.substring(oldindex,_ NewINdex-Oldindex)
ListBox1.items.add(tempWord)
newIndex = newIndex + advanceSize
oldIndex = newIndex


End If



Loop
End Sub



Private Sub buildButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buildButton.Click


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

End Sub

Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
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
Mar 28 '07 #1
1 3029
you could try split instead

syntax:

Dim MyList() as string = Mychardelimitedstring.Split(yourdelimiter)

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
Mar 28 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
10
by: micklee74 | last post by:
hi if i have a some lines like this a ) "here is first string" b ) "here is string2" c ) "here is string3" When i specify i only want to print the lines that contains "string" ie the first...
5
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
3
by: crprajan | last post by:
String Manipulation: Given a string like “This is a string”, I want to remove all single characters( alphabets and numerals) like (a, b, 1, 2, .. ) . So the output of the string will be “This is...
7
Frinavale
by: Frinavale | last post by:
I currently have a .NET application that has an object which passes a string (a connection string) as a parameter to another object that does database manipulation. This string isn't stored...
3
by: frankeljw | last post by:
I have 2 Java strings 1st String is a series of names, colons, and numbers ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:Name6:44 2nd String is a name ie) Name2 I need to get the...
22
by: mann_mathann | last post by:
can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.