Presentation is loading. Please wait.

Presentation is loading. Please wait.

There is a String type in VB Data TypePrefix Stringstr Dim strFirstName As String strFirstName = “Homer” strFirstName = “” String A String variable can.

Similar presentations


Presentation on theme: "There is a String type in VB Data TypePrefix Stringstr Dim strFirstName As String strFirstName = “Homer” strFirstName = “” String A String variable can."— Presentation transcript:

1 There is a String type in VB Data TypePrefix Stringstr Dim strFirstName As String strFirstName = “Homer” strFirstName = “” String A String variable can hold strings of arbitrary length All punctuation marks, and digits are included. “124!@#_-=+)),”

2 Dim strFirstName As String Dim strLastName As String Dim strFullName As String strFirstName = “Homer” strLastName = “Simpson” strFullName = strFirstName & “ ” & strLastName bntRedDemo.Text = strFullName

3 Using Subroutines/Methods/Functions The black box view of a function 2 3 6 2 58

4 Here is a different function.. “Cat” 2 “Dog” 1 “Ca”“D”

5 What do we need, to specify a function? A name A parameter list. A list of the “things” the functions expects, in this case, a string, followed by an integer. A return type. In this case a string “Cat” 2 “Ca” Truncate

6 Visual Basic has built in functions (and later we can write our own) Each function has: A name A parameter list A return type In this case, the name is UCase, the parameter list is a single string and the return type is a single string. “Cat” “CAT” UCase strFirstName = "Homer" bntRedDemo.Text = UCase(strFullName)

7 Function call Return Value UCase(“Input String”) “INPUT STRING” UCase(“all lowercase”) “ALL LOWERCASE” UCase(“ALL UPPERCASE”) “ALL UPPERCASE” UCase(“UpPeP AnD lOwErCaSE”) “UPPER AND LOWERCASE” strFirstName = "Homer" bntRedDemo.Text = UCase(strFullName) bntRedDemo.Text = UCase(“marge”) strFName = “John” strLName = “Doe” bntRedDemo.Text = UCase(strFName & “ ” & strLName ) Syntax: String = UCase(String)

8 strA = …. If (strA = “Male” Or strA = “male” Or strA = “MALE” ) Then bntRedDemo.Text = “You choose male.” End If strA = …. If (Ucase(strA) = “MALE”) Then bntRedDemo.Text = “You choose male.” End If A classic use of the UCase function is robust data entry…

9 strFName = “John” strLName = “Doe” bntRedDemo.Text = LCase(strFName & “ ” & strLName ) Function call Return Value LCase(“Input String”) “input string” LCase(“all lowercase”) “all lowercase” LCase(“ALL UPPERCASE”) “all uppercase” LCase(“UpPeP AnD lOwErCaSE”) “upper and lowercase” There a complimentary function to UCase called LCase. It works exactly like you would expect … Syntax: String = LCase(String)

10 strAge = “5” strAgeDiff = 100 – Val(strAge) There a function call Val, which converts strings to numbers. Syntax: Numeric Value = Val(String) Function call Return Value Val(“199.11”) 199.11 Val(“ 199.11 “) 199.11 Val(“ 1 99.1 1”) 199.11 Val(“ 199 “) 199 Val(“$199.11”) 0 Val(“1,199.11”) 1 Val(“ “) 0 Val(“123abc”) 123 Val(“abc123”) 0 Function Name: Val Function Description: Returns a numeric representation of the String value passed to it. Val will convert a String to a numeric until it reaches a character that is not a numeric value, a decimal point, or a white-space character. Once an unrecognizable character is read, conversion stops at that point. Syntax: Numeric Value = Val(String) Examples:

11 bntRedDemo.Text = Str(25) bntRedDemo.Text = (25).ToString There a complementary function call Str, which converts numbers to strings. Syntax: String = Str(Numeric Value) Function Name: Str Function Description: Returns a String representation of the numeric value passed to it. By default it will place a single space in front of the first numeric character. These are logically equivalent (almost!)

12 There is a function called Trim, which removes trailing and leading space from text. Function call Return Value Trim(“ InputString”) “InputString” Trim(“InputString ”) “InputString” Trim(“ InputString ”) “InputString” Trim(“ Input String ”) “Input String” strA = “ Male” If (strA = “Male”) Then bntRedDemo.Text = “You choose male.” End If If (Trim(strA) = “Male”) Then This is False! Syntax: String = Trim(String) This is True

13 We can nest functions... strA = “ Male” If ( Trim(UCase(strA) ) = “MALE”) Then bntRedDemo.Text = “You choose male.” End If Trim(Ucase(“ Male”)) Trim(Ucase(strA)) Trim(“ MALE”) “MALE”

14 We can nest functions... bntRedDemo.Text = Str(5) & Str(5) bntRedDemo.Text = Str(5) & Trim(Str(5))

15 There is a function called Len, which returns the number of characters contained in a String strA = … If ( Len(strA) > 10 ) Then bntRedDemo.Text = “You have a long name!” Else If ( Len(strA) <= 1 ) ‘The DMV requires at least 2 letters bntRedDemo.Text = “This is not a legal name!” End If Syntax: Integer = Len(String)

16 There is a function called Mid, which returns a subsection of a string… Function call Return Value Mid(“This is the String”, 6, 2) “is” Mid(“This is the String”, 9, 3) “the” Dim shtP As Short = 4 Mid(“This is the String”, 13, shtP ) “Stri” Mid(“This is the String”, 8) “ the String” Syntax: String = Mid(String, integer_type, integer_type ) Optional! Returns a specific number of characters of a String allowing the developer to indicate where to start and how many characters to return. The first parameter is the source String. The second is an Integer indicating the starting position to copy from. The third parameter is optional and indicates the number of characters to copy. If the third parameter is left out, all characters from the starting position are returned.

17 By using functions we can do lots of cool things… Suppose I want to get just the First letter in someone's name… Dim strFName, strLName, strMName, strFullName As String strFName = “Homer” strLName = “Simpson” strMName = “Jay” strFullName = strFName & " " & Mid(strMName, 1, 1) & " " & strLName bntRedDemo.Text = strFullName

18 By using functions we can do lots of cool things… Suppose I want to get just the Last letter in a word… Dim strW, strLastLetter As String strW = “Books” strLastLetter = Mid(strW,len(strW),1) If ( (UCase( strLastLetter )) = “S”) Then bntRedDemo.Text = “The word is probably plural” End If


Download ppt "There is a String type in VB Data TypePrefix Stringstr Dim strFirstName As String strFirstName = “Homer” strFirstName = “” String A String variable can."

Similar presentations


Ads by Google