Basic String Functions

Snippets, Projects and Assistance

Moderators: Moderator, Global Moderator

Post Reply
FlyingDoggyJake
Sr. Member
Sr. Member
Posts: 254
Joined: Wed Jun 16, 2004 5:37 pm

Basic String Functions

Post by FlyingDoggyJake »

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.
Last edited by FlyingDoggyJake on Sun Sep 26, 2004 7:07 pm, edited 1 time in total.
ImageImage
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 7:54 am

Basic String Functions

Post by Josh »

*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]

Image

“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
Sr. Member
Posts: 254
Joined: Wed Jun 16, 2004 5:37 pm

Basic String Functions

Post by FlyingDoggyJake »

Glad you find it helpful Josh.... good to hear from you too /smile.gif' class='bbc_emoticon' alt=':)' />

Now on with the tutorial:

The InStr Function

I'll now discuss the InStr function. This is a useful function to detect certain sequences of characters within inputs from various sources. To properly cover how to use the InStr function we'll also have to cover the If selection structure and UCase / LCase 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 Private Sub Command1_Click() we could have the following code:

Private Sub Command1_Click()

' 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

If InStr(1, UCase(Text1.Text), "FLYINGDOGGYJAKE") Then
MsgBox "Hey, you're stealing my nick!"
Text1.Text = ""
Exit Sub
End If


Step by Step

The first parameter of the InStr function was 1. This simply states that we want to start looking for "FLYINGDOGGYJAKE" starting at the very first character of the text in the Text1 textbox.

In the second parameter we opted to use the UCase function. The UCase 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 InStr function began looking.

The third parameter is the actual text that we are looking for. Because we used the UCase 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 LCase function and searched for "flyingdoggyjake" instead.

Well that about covers the InStr function /smile.gif' class='bbc_emoticon' alt=':)' />
ImageImage
Post Reply

Return to “Visual Basic”