Showing posts with label Strings. Show all posts
Showing posts with label Strings. Show all posts

Friday, February 29, 2008

If Statements and Strings

We are able to compare strings using If statements. This means that we can see if two strings are the same string [i.e. "george" = "george"], but more importantly we are able to compare the relationship between strings. That is, we can tell if a certain string comes before another string:

Dim strA, strB As String
strA = "cat"
strB = "dog"

If strA < strB Then
MsgBox(strA & " comes before " & strB, "")
ElseIf strA > strB Then
MsgBox(strA & " comes after " & strB, "")
Else
MsgBox(strA & " equals " & strB, "")
End If


Gives answer of "cat comes before dog"

One important thing to remember, however is that capital letters come before lowercase letters. This is because of the ASCII/ANSI coding of each letter in the computer.

The computer does not understand the word "cat" as we humans do. For the computer each letter has a binary value [binary = 1's and 0's]. Letters are given numbers in sequential order from 65-90 for A-Z and 97-122 for a-z. Since the capital letters have lower values, they are less than the lowercase letters and therefore "come first."


Dim strA, strB As String
strA = "cat"
strB = "Zebra"

If strA < strB Then
MsgBox(strA & " comes before " & strB, "")
ElseIf strA > strB Then
MsgBox(strA & " comes after " & strB, "")
Else
MsgBox(strA & " equals " & strB, "")
End If


Gives answer of "Zebra comes before cat"

If you want to eliminate case, you can use the .ToUpper and .ToLower functions.

Thursday, February 28, 2008

String formatting

As promised, here are the notes on section 3.5 Input/Output (by Alex)
________________________________________________________________

Formatting:

There are various ways of formatting output in VB. One way is with some more built in functions:

FormatNumber( number or variable to format, places to round )

This will take a number and format it to a certain number of decimal places. Below is an example of using this function:

FormatNumber( 12345.628, 2) will evaluate to 12,345.63
FormatNumber( 444212.333, 0) will evaluate to 444,212

FormatCurrency( number or variable to format, places to round )

This will take a number and format it to a currency value to a certain number of decimal places. Below is an example of using this function:

FormatCurrency( 12345.628, 2) will evaluate to $12,345.63
FormatCurrency( 345.4426, 3) will evaluate to $345.443

FormatPercent( number or variable to format, places to round )

This will take a number and format it to a percentage value to a certain number of decimal places. Below is an example of using this function:

FormatPercent( 0.185, 2) will evaluate to 18.50%


Now, notice with the Percent and Currency function, the appropriate symbol is added to the outputted value. There are other Format functions available but these are again the most frequently used.

To format a string output, you use something called zones. Since I cannot draw once again, I will have to refer you to the textbook example 1 in section 3.5. But here is how the formatting variable will look:

Dim fmtStr As String
fmtStr = "{0,15}{1,8}{2,9}"

Here is how that looks. Notice that there is no spaces between the curly braces inside the string. Also notice each section has two numeric values. The first value is the zone number so in the above example they would be 0...1...2. The second value is the number of spaces to format so these would be 15...8...9. Here is how to use it in a program:

lstOut.Items.Add( String.Format( fmtStr,item 1, item 2, item 3 ) )

The number of items in the above line needs to match the number of zones you declare in the formatting string. Again, see example 1 for the full diagram as well as a short program.

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.