18
There is a String type in There is a String type in VB VB Data Type Prefix String str 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!@#_-=+)),”

There is a String type in VB

  • Upload
    zwi

  • View
    31

  • Download
    0

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: There is a String type in VB

There is a String type in VBThere is a String type in VB

Data Type Prefix

String str

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!@#_-=+)),”

Page 2: There is a String type in VB

Dim strFirstName As String Dim strLastName As StringDim strFullName As String

strFirstName = “Homer”strLastName = “Simpson”

strFullName = strFirstName & “ ” & strLastName

bntRedDemo.Text = strFullName

Page 3: There is a String type in VB

Using Subroutines/Methods/FunctionsUsing Subroutines/Methods/Functions

The black box view of a function

2 3 6 2

5 8

Page 4: There is a String type in VB

Here is a different function..

“Cat” 2 “Dog” 1

“Ca” “D”

Page 5: There is a String type in VB

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

Page 6: There is a String type in VB

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)

Page 7: There is a String type in VB

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)Syntax: String = UCase(String)

Page 8: There is a String type in VB

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…

Page 9: There is a String type in VB

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)Syntax: String = LCase(String)

Page 10: There is a String type in VB

strAge = “5”

strAgeDiff = 100 – Val(strAge)

There a function call Val, which converts strings to numbers.

Syntax: Numeric Value = Val(String)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:

Page 11: There is a String type in VB

bntRedDemo.Text = Str(25)

bntRedDemo.Text = (25).ToString

There a complementary function call Str, which converts numbers to strings.

Syntax: String = Str(Numeric Value)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!)

These are logically equivalent (almost!)

Page 12: There is a String type in VB

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!This is False!

Syntax: String = Trim(String)Syntax: String = Trim(String)

This is TrueThis is True

Page 13: There is a String type in VB

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”

Page 14: There is a String type in VB

We can nest functions...

bntRedDemo.Text = Str(5) & Str(5)

bntRedDemo.Text = Str(5) & Trim(Str(5))

Page 15: There is a String type in VB

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)Syntax: Integer = Len(String)

Page 16: There is a String type in VB

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 )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.

Page 17: There is a String type in VB

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

Page 18: There is a String type in VB

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