Monday, February 11, 2008

String Lecture notes (from Alex)

I am available for tutoring in A205. My schedule is posted online at the department's website.
_____________________________________________________________________________

Formally, a string is a sequence of characters that can be treated as a single item. In other words, anything and everything together. Here are some examples:

"34huwnacbui23h4r"
"The quick brown fox."
"My name is: John Doe"

Here is how to declare a string variable:

Dim varName As String

By default, the string variable is automatically set to empty, or Nothing.

(See example 1 in 3.4 for a basic example)

For the purpose of the HW assignment (37-40 in section 3.4), you were gathering certain information from textboxes. Again, by default, anything you gather from a textbox is a stored as a string. So as a brief example, say you entered a number and tried to place it in the appropriate variable. This will not work since a string cannot be placed into a variable declared of a numeric type. There is a way to fix this. In VB, there are built-in functions that will correctly convert a string to a number or certain other variable types. Below are some examples of that:

CDbl() -- means Convert to a double. This is the most commonly seen conversion function in the textbook since most variables are declared as a Double type. This will convert the string entered into a double.

CInt() -- means Convert to an integer. Will take a string and convert to the Integer type.

CLng() -- means Convert to a Long integer. Will take a string and convert to the Long type.

CStr() -- means Convert to a String. Yes, there is a function that will "go the other way" so to speak. This will be used for output purpose mostly though there may be an occasion to use for input purpose.

Below is a short program to demonstrate some conversion functions:

Private Sub btnAdd_Click(...) Handles btnAdd.Click

'This program will gather two numbers from the user and output the sum
Dim num1, num2, sum As Double

num1 = CDbl(txtNum1.Text)
num2 = CDbl(txtNum2.Text)

sum = num1+num2

txtOut.Text = CStr("The sum is: " & sum)

End Sub

Now, just one note from the above program. In the last line, you see a & symbol. This is called the concatenation symbol. It will join two or more pieces of data together. So in this case, the user will see the following message in the textbox if the sum was 7: The sum is: 7 Concatenation will be seen quite frequently for the rest of the semester.

(See example 3 for an example of concatenation).

STRING FUNCTIONS:

In VB, there are certain functions that you can apply to strings. Below are some examples using them. For the purpose of all the examples below, use the following definition of a string:

Dim str As String

str.Length()
-will return the length of a string as an integer. Say the string was "Hello". You will get an answer of 5.

str.ToUpper()
-will change the string to all uppercase characters (not numbers or symbols). Say the string is "Hello James 1234!", the function will change this to "HELLO JAMES 1234!"

str.ToLower()
-just the opposite of ToUpper()

str.Trim()
-will eliminate all the white spaces at the front and end of a string. Say the string is " Hello "
Applying this function, you will get "Hello"

The following two functions contain something called parameters (explained more in chapter 4). These are data values that are required for the function. Also, these functions require the knowledge of how a string looks in memory. Since I cannot draw with the blog, I will do my best to show it. The pipe symbol | will be used to separate the individual characters:

Say you declare a string variable and give it the value, somewhere in the program, "John". Here is how it would look:

J | o | h | n | \0
0 | 1 | 2 | 3 | 4

The numbers appearing below the string are called indices's. They are the "position" of the certain character. The \0 is called the terminating character or the null string. This is basically the end of the string. So let's look at the function:

str.IndexOf( string ) OR str.IndexOf( string, starting position ).

This function will return the index of the FIRST OCCURRENCE of a string you give it to find. If it cannot find it, it will give you -1. So let's use the string "fanatic". Looking at it in memory (this is the best I could do):

f | a | n | a | t | i | c | \0
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7

Let's apply the function.

str.IndexOf("ati") will give you an answer of 3 since the first occurrence of this string begins at position, or index of 3.

str.IndexOf("a") will give you an answer of 1 since the first occurrence of this string begins at position, or index, 1.

str.IndexOf("nt") will give you an answer of -1 since this string does not exist!

str.IndexOf("a", 4) will give you an answer of -1 since starting at position 4, there is no matching string.

Now let's look at the following function called the sub string:

str.SubString( starting position, how many places to move ) OR str.SubString( starting position )

This function will now return a portion of a string instead of the index of a portion of a string. If you do not specify the second parameter, it will go all the way to the end of a string. So now let's use the "fanatic" string again.

str.SubString( 0, 3 ) will return "fan" since beginning at position 0 and going 3 places you get "fan"

str.SubString( 4, 2) will return "ti" since beginning at position 4 and going 2 places, you get "ti"

str.SubString( 4 ) will return "tic" since beginning at position 4 and going to the end you get "tic"

(See example 5 in 3.4 for a great example of the previous string functions).

For what we covered on Input Boxes and Message Boxes, please refer to the end of that chapter since I cannot draw on the blog. If there are any concerns about them, please let me know next week.

No comments: