Interesting Topic - Programming Strings!!!

Started by sukishan, Jul 11, 2009, 06:09 PM

Previous topic - Next topic

sukishan

Programming Strings

What is a String?
Actually a string is nothing more than text. And if we want Visual Basic to store text we need to use the word "String". To set up a variable to hold text we need to use As String and not As Integer. If the information we want to store in our variables is a First Name and a Last Name, we can set up two variables like this.

Dim FirstName As String
Dim LastName As String

Again we've started with the Dim word. Then we've called the first variable FirstName. Finally, we've ended the line by telling Visual Basic that we want to store text in the variable - As String.

So we've set up the variables. But there is nothing stored in them yet. We store something in a variable with the equals sign ( = ). Let's store a first name and a last name in them

FirstName = "Bill"
LastName = "Gates"

Here, we said to Visual Basic "Store the word 'Bill' into the variable FirstName and store the word 'Gates' into the variable called LastName. But pay attention to the quotation marks surrounding the two words. We didn't say Bill, we said "Bill". Visual Basic needs the two double quotation marks before it can identify your text, your String.

So remember: if you're storing text in a variable, don't forget the quotation marks!

To test all this out, add a new Button to your Form. Set the Text property of the Button to "String Test". Your Form would then look like this:

Double click your new button, and add the following code:

Dim FirstName As String
Dim LastName As String
Dim FullName As String

FirstName = "Bill"
LastName = "Gates"

FullName = FirstName & LastName

Textbox1.Text = FullName

Your code window should now look like this (some of the first line has been cropped in the image below):

There's a line there that needs explaining

FullName = FirstName & LastName

In the two lines of code above that one, we stored the string "Bill" and the string "Gates" into two variables. What we're doing now is joining those two variables together. We do this with the ampersand symbol ( & ). The ampersand is used to join strings together. It's called Concatenation.

Once Visual Basic has joined the two strings together (or concatenated them), we're saying "store the result in the variable called FullName". After that, we tell VB to display the result in our Textbox.

So, once you've typed the code, start your programme and test it out.

Once the programme is running, Click the Button and see what happens. You should have a Form that looks something like this one:

The textbox displays the text stored in our variables, "Bill" and "Gates". We joined them together with the ampersand ( & ). But as you can see, the two words are actually joined as one. We can add a bit of space between the two words by using another ampersand. Change this line FullName = FirstName & LastName to this:

FullName = FirstName & " " & LastName

What we're saying here is join this lot together: the variable called FirstName and a single blank space and the variable called LastName. When you've finished concatenating it all, store the result in the variable FullName.

Notice that we don't surround FirstName and LastName with quotation marks. This is because these two are already string variables; we stored "Bill" into FirstName and "Gates" LastName. So VB already knows that they are text.
A good beginning makes a good ending