Basic String Functions
Posted: Sat Sep 25, 2004 6:31 pm
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.
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.