Basic String Functions
Posted: Sat Sep 25, 2004 11:31 am
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 General Declerations or at the beginning of a Sub or Function. When declared in General Declerations the variable has scope through out the entire module. When declared at the beginning of a Sub the variable only has scope within that Sub. When declaring a variable in General Declerations you also have the option of declaring the variable as Public which means the variable will have scope throughout all modules. Anyway, back to string manipulation:
' Declare our variable
Dim ourstring As String
' Declare a second variable
Dim resultstring As String
'give our string variable a value
ourstring = "This is our example string! Visual Basic is great!"
The Mid Function
'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
resultstring = Mid(ourstring, 1, 4)
In this example the variable resultstring would now hold the string "This"
Why?
The first parameter of the Mid function asks for the string that we want to manipulate. So we entered ourstring which was holding the value "This is our example string! Visual Basic is great!".
The next parameter of the Mid function requires us to specify at which character in the string (ourstring) we want to start our new string (resultstring). We specified 1 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 Mid function tells how many characters long we would like to use from the old string. We specified 4 which means we will use 4 characters in total from the old string (ourstring).
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 (resultstring) becomes "This".
The Split Function
'The Split function is usefull in many data transfer applications and other strings that adhere to particular formats
'using the example of ourstring
resultstring = Split(ourstring, "!")(1)
Remember that the value of ourstring is "This is our example string! Visual Basic is great!". After the above code, the value of resultstring would now be " Visual Basic is great"
Why?
The first parameter of the Split function is the old string that we want to use to manipulate into a new string. Once again we used ourstring as our old string.
The second parameter is called the Delimiter. The Split 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 0 became "This is our example string" and segment 1 became " Visual Basic is great".
When using the Split 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 (1).
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 General Declerations or at the beginning of a Sub or Function. When declared in General Declerations the variable has scope through out the entire module. When declared at the beginning of a Sub the variable only has scope within that Sub. When declaring a variable in General Declerations you also have the option of declaring the variable as Public which means the variable will have scope throughout all modules. Anyway, back to string manipulation:
' Declare our variable
Dim ourstring As String
' Declare a second variable
Dim resultstring As String
'give our string variable a value
ourstring = "This is our example string! Visual Basic is great!"
The Mid Function
'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
resultstring = Mid(ourstring, 1, 4)
In this example the variable resultstring would now hold the string "This"
Why?
The first parameter of the Mid function asks for the string that we want to manipulate. So we entered ourstring which was holding the value "This is our example string! Visual Basic is great!".
The next parameter of the Mid function requires us to specify at which character in the string (ourstring) we want to start our new string (resultstring). We specified 1 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 Mid function tells how many characters long we would like to use from the old string. We specified 4 which means we will use 4 characters in total from the old string (ourstring).
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 (resultstring) becomes "This".
The Split Function
'The Split function is usefull in many data transfer applications and other strings that adhere to particular formats
'using the example of ourstring
resultstring = Split(ourstring, "!")(1)
Remember that the value of ourstring is "This is our example string! Visual Basic is great!". After the above code, the value of resultstring would now be " Visual Basic is great"
Why?
The first parameter of the Split function is the old string that we want to use to manipulate into a new string. Once again we used ourstring as our old string.
The second parameter is called the Delimiter. The Split 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 0 became "This is our example string" and segment 1 became " Visual Basic is great".
When using the Split 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 (1).
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.