I thought that I might make a quick tutorial on how to manipulate strings.
Let\'s start by declaring a variable as a string (Depending on the scope you want this can be done in [color=\"blue\"]General Declerations[/color] or at the beginning of a [color=\"blue\"]Sub[/color] or [color=\"blue\"]Function[/color]. When declared in [color=\"blue\"]General Declerations[/color] the variable has scope through out the entire module. When declared at the beginning of a [color=\"blue\"]Sub[/color] the variable only has scope within that [color=\"blue\"]Sub[/color]. When declaring a variable in [color=\"blue\"]General Declerations[/color] you also have the option of declaring the variable as [color=\"blue\"]Public[/color] which means the variable will have scope throughout all modules. Anyway, back to string manipulation:
[color=\"green\"]\' Declare our variable[/color]
[color=\"blue\"]Dim ourstring As String[/color]
[color=\"green\"]\' Declare a second variable[/color]
[color=\"blue\"]Dim resultstring As String[/color]
[color=\"green\"]\'give our string variable a value[/color]
[color=\"blue\"]ourstring = \"This is our example string! Visual Basic is great!\"[/color]
[size=7][color=\"red\"][u]The Mid Function[/u][/size][/color]
[color=\"green\"]\'the mid function can be used to make a new string from a particular string segment of an existing string. This is done based on position of characters[/color]
[color=\"blue\"]resultstring = Mid(ourstring, 1, 4)[/color]
In this example the variable [color=\"blue\"]resultstring[/color] would now hold the string \"This\"
Why?
The first parameter of the [color=\"blue\"]Mid[/color] function asks for the string that we want to manipulate. So we entered [color=\"blue\"]ourstring[/color] which was holding the value \"This is our example string! Visual Basic is great!\".
The next parameter of the [color=\"blue\"]Mid[/color] function requires us to specify at which character in the string ([color=\"blue\"]ourstring[/color]) we want to start our new string ([color=\"blue\"]resultstring[/color]). We specified [color=\"blue\"]1[/color] which means we want our new string to begin at the very start of the old string. The first character of the old string is \"T\" so we now know that our new string will start with a \"T\".
The final parameter of the [color=\"blue\"]Mid[/color] function tells how many characters long we would like to use from the old string. We specified [color=\"blue\"]4[/color] which means we will use 4 characters in total from the old string ([color=\"blue\"]ourstring[/color]).
We already know that the new string will start on the character 1 of ourstring and that the new string will be 4 characters long. Thus our new string ([color=\"blue\"]resultstring[/color]) becomes \"This\".
[size=7][color=\"Red\"][u]The Split Function[/u][/size][/color]
[color=\"green\"]\'The Split function is usefull in many data transfer applications and other strings that adhere to particular formats
\'using the example of ourstring[/color]
[color=\"blue\"]resultstring = Split(ourstring, \"!\")(1)[/color]
Remember that the value of ourstring is \"This is our example string! Visual Basic is great!\". After the above code, the value of [color=\"blue\"]resultstring[/color] would now be \" Visual Basic is great\"
Why?
The first parameter of the [color=\"blue\"]Split[/color] function is the old string that we want to use to manipulate into a new string. Once again we used [color=\"blue\"]ourstring[/color] as our old string.
The second parameter is called the [color=\"blue\"]Delimiter[/color]. The [color=\"blue\"]Split[/color] will look for the delimiter in the old string and chop the old string into segments between each delimiter. So in our example we used \"!\" as our delimiter so segment [color=\"blue\"]0[/color] became \"This is our example string\" and segment [color=\"blue\"]1[/color] became \" Visual Basic is great\".
When using the [color=\"blue\"]Split[/color] function you specify the segment which you wish to use in a set of brackets after the closing parameter brackets. In our example we specified [color=\"blue\"](1)[/color].
NOTE: You must be careful to either properly handle errors when using the Split function or make sure that you always have enough Delimiters in any string you use as your first parameter of the Split function. Otherwise your program will have a runtime error.
When I get more time I\'ll move onto other functions such as InStr, Right ... etc.
Basic String Functions
Moderators: Moderator, Global Moderator
-
FlyingDoggyJake
- Sr. Member

- Posts: 254
- Joined: Thu Jun 17, 2004 12:37 am
Basic String Functions
Last edited by FlyingDoggyJake on Mon Sep 27, 2004 2:07 am, edited 1 time in total.


Basic String Functions
*bows before jake* thanks for making this post
/blum.gif\' class=\'bbc_emoticon\' alt=\':P\' /> I hate searching for things like these and have to read each thing on 16 different sites lol
[align=center]

“If you stop learning, you stop living.” ~Tami Quiring
“It's the rare man who understands the value of a single perfect rose.”
[/align]

“If you stop learning, you stop living.” ~Tami Quiring
“It's the rare man who understands the value of a single perfect rose.”
[/align]
-
FlyingDoggyJake
- Sr. Member

- Posts: 254
- Joined: Thu Jun 17, 2004 12:37 am
Basic String Functions
Glad you find it helpful Josh.... good to hear from you too
/smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />
Now on with the tutorial:
[size=7][color=\"red\"][u]The InStr Function[/u][/size][/color]
I\'ll now discuss the [color=\"blue\"]InStr[/color] function. This is a useful function to detect certain sequences of characters within inputs from various sources. To properly cover how to use the [color=\"blue\"]InStr[/color] function we\'ll also have to cover the [color=\"blue\"]If[/color] selection structure and [color=\"blue\"]UCase / LCase[/color] functions.
Let\'s suppose our program requires an input from the user. For example our program states \"Please enter your name:\" and there\'s a blank text box waiting for the user to enter their name. There is also a Command button with a caption that says \"Enter\".
Now in [color=\"blue\"]Private Sub Command1_Click()[/color] we could have the following code:
[color=\"blue\"]Private Sub Command1_Click()[/color]
[color=\"green\"]\' we can use the InStr function to make sure that the user will not enter a particular sequence of characters, For example your own nickname[/color]
[color=\"blue\"]If InStr(1, UCase(Text1.Text), \"FLYINGDOGGYJAKE\") Then
MsgBox \"Hey, you\'re stealing my nick!\"
Text1.Text = \"\"
Exit Sub
End If[/color]
[color=\"red\"][u]Step by Step[/u][/color]
The first parameter of the InStr function was [color=\"blue\"]1[/color]. This simply states that we want to start looking for \"FLYINGDOGGYJAKE\" starting at the very first character of the text in the [color=\"blue\"]Text1[/color] textbox.
In the second parameter we opted to use the [color=\"blue\"]UCase[/color] function. The [color=\"blue\"]UCase[/color] function has a single string parameter and it simply changes all the characters of its paramater string to uppercase. So in our example if the user entered \"FlYInGdoGGyjAke\" it would automatically get changed to \"FLYINGDOGGYJAKE\" before the [color=\"blue\"]InStr[/color] function began looking.
The third parameter is the actual text that we are looking for. Because we used the [color=\"blue\"]UCase[/color] function we know that we will be looking for something that is completely in uppercase letters. That is why we chose to look for \"FLYINGDOGGYJAKE\" as apposed to \"flyingdoggyjake\". However we could have just as easily used the [color=\"blue\"]LCase[/color] function and searched for \"flyingdoggyjake\" instead.
Well that about covers the [color=\"blue\"]InStr[/color] function
/smile.gif\' class=\'bbc_emoticon\' alt=\':)\' />
Now on with the tutorial:
[size=7][color=\"red\"][u]The InStr Function[/u][/size][/color]
I\'ll now discuss the [color=\"blue\"]InStr[/color] function. This is a useful function to detect certain sequences of characters within inputs from various sources. To properly cover how to use the [color=\"blue\"]InStr[/color] function we\'ll also have to cover the [color=\"blue\"]If[/color] selection structure and [color=\"blue\"]UCase / LCase[/color] functions.
Let\'s suppose our program requires an input from the user. For example our program states \"Please enter your name:\" and there\'s a blank text box waiting for the user to enter their name. There is also a Command button with a caption that says \"Enter\".
Now in [color=\"blue\"]Private Sub Command1_Click()[/color] we could have the following code:
[color=\"blue\"]Private Sub Command1_Click()[/color]
[color=\"green\"]\' we can use the InStr function to make sure that the user will not enter a particular sequence of characters, For example your own nickname[/color]
[color=\"blue\"]If InStr(1, UCase(Text1.Text), \"FLYINGDOGGYJAKE\") Then
MsgBox \"Hey, you\'re stealing my nick!\"
Text1.Text = \"\"
Exit Sub
End If[/color]
[color=\"red\"][u]Step by Step[/u][/color]
The first parameter of the InStr function was [color=\"blue\"]1[/color]. This simply states that we want to start looking for \"FLYINGDOGGYJAKE\" starting at the very first character of the text in the [color=\"blue\"]Text1[/color] textbox.
In the second parameter we opted to use the [color=\"blue\"]UCase[/color] function. The [color=\"blue\"]UCase[/color] function has a single string parameter and it simply changes all the characters of its paramater string to uppercase. So in our example if the user entered \"FlYInGdoGGyjAke\" it would automatically get changed to \"FLYINGDOGGYJAKE\" before the [color=\"blue\"]InStr[/color] function began looking.
The third parameter is the actual text that we are looking for. Because we used the [color=\"blue\"]UCase[/color] function we know that we will be looking for something that is completely in uppercase letters. That is why we chose to look for \"FLYINGDOGGYJAKE\" as apposed to \"flyingdoggyjake\". However we could have just as easily used the [color=\"blue\"]LCase[/color] function and searched for \"flyingdoggyjake\" instead.
Well that about covers the [color=\"blue\"]InStr[/color] function

