473,785 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Manipulation

9 New Member
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
Mar 28 '07 #1
1 3062
pongscript
27 New Member
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
Mar 28 '07 #2

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

Similar topics

4
2275
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 values and separators class { private string globalvar = ""; private void manipulate (whattodo ) //substring, join, etc....
32
14899
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
29
4324
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 substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can any tell me how I can implement such functionality in C#? 2. Is it possible to add/include function...
4
3493
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 substrings need be replaced, or one character needs be changed, what shall I do? Is it better to convert strings to UCS-32 before manipulation? But on Windows, wchar_t is 16 bits which isn't enough for characters which can't be simply encoded...
10
1776
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 line and not the others. If i use re module, how to compile the expression to do this? I tried the re module and using simple
5
7495
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 20.02.2006 etc... And I want to replace the /,.etc character with - (as 20-02-2006)
3
1566
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 string” This is very urgent. Please help
7
2107
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 anywhere else and is only used by this behind-the-scenes object to provide the database manipulation object with a connection string. Does my connection string pose a security problem when it is inside the code like this? Or are connection strings...
3
3567
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 number associated with that name.
22
2625
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
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.