Chapter 4 (I don’t have the title)

Download Report

Transcript Chapter 4 (I don’t have the title)

Strings(Part 1)
1. String Literal and String Variable
2. Declaration and Initialization
3. Using String in Input and Output
String
• String literal
a sequence of characters treated as a
single item, surrounded by “”
• String variable
a name used to refer to a string
7/20/2015
Strings
2
String declaration and
Initialization
• Declaration
Dim varName As String
• Initialization
Dim today As String = "Monday“
Dim today As String = "“
• A string should be assigned a value
before being used .By default the
initial value is Nothing
7/20/2015
Strings
3
Using Strings
• Using Text Boxes for Input and Output
• The contents of a text box is a string
• input
strVar = txtBox.Text
• Output (ReadOnly = true)
txtBox.Text = strVar
• String literal can be displayed in list box
lstBox.Items.Add(“Monday”)
7/20/2015
Strings
4
Data Conversion(Type – Casting)
• Conversion from one dataType to
another, Such as
numVar = CDbl(txtBox.Text)
numVar = CSng(txtBox.Text)
numVar = CInt(txtBox.Text)
txtBox.Text = CStr(numVar)
• All conversions to String are
considered to be widening, regardless
of whether strict semantics are used
7/20/2015
Strings
5
Code
7/20/2015
Strings
6
Code
' The celsius temperature is converted into Fahrenheit
Private Sub btnConvert_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnConvert.Click
Dim tempC As Single
Dim tempF As Single
Dim tempFStr As String
' Get the temp from user
tempC = CSng(txtTemp.Text)
' Convert tempC to TempF
tempF = (tempC * 9 / 5) + 32
' Display tempF in the list box
tempFStr = CStr(tempF)
lstTemp.Items.Add(tempFStr)
End Sub
7/20/2015
Strings
7
Concatenation
• Combining two strings into a new
string.Concatenation is represented
by “&”
Dim str1 As String = “My grade "
Dim Str2 As String = “is A"
txtOutput.Text = str1 & str2
My grade is A
7/20/2015
Strings
8
Concatenation
• Combining strings with numbers into a
string
Dim str As String = “My grade is “
Dim grade As Double = 89
txtOutput.Text = str & grade
displays
My grade is 89
7/20/2015
Strings
9
Code
• Editing code
lstTemp.Items.Add( “ The temperature is “ & tempFStr & “F”)
Displays
The temperature is 80F
7/20/2015
Strings
10
ANSI Character Set
• A numeric representation for every key on
the keyboard. The numbers are ranging
from 32 to 255
32 (space)
33 !
34 “
35 #
7/20/2015
48 0
49 1
57 9
65 A
66 B
90 Z
97 a
98 b
122 z
123 {
125 }
126 -
Strings
11
ANSI Character Set
• If n is between 32 and 255, str
Chr(n):returns the string consisting of the
character with ANSI value n
Chr(65) ----- A
Asc(str):returns the ANSI value of the first
character of str
Asc(“Apple”) --- 65
32 & Chr(176) & “Fahrenheit -- 32° Fahrenheit
7/20/2015
Strings
12
Strings(Part 2)
1. String Properties and Methods
2. Option Statements
3. Internal Documentation
String Properties and
Methods
• A string is an object, like controls, has
both properties and methods.
•Length
•ToUpper
•Trim
•ToLower
•IndexOf
•Substring
7/20/2015
Strings
14
String Properties (Length)
• str.Length:
the number of characters in str
Dim str As String = “Tacoma“
str.Length is 6
7/20/2015
Strings
15
ToUpper and ToLower
• str.ToUpper
with all letters of str capitalized
str.ToUpper() is TACOMA.
• str.ToLower
with all letters of str in lowercase
format
str.ToLower() is tacoma
7/20/2015
Strings
16
Trim, TrimStart, TrimEnd
• str.Trim
with all leading and trailing spaces
deleted
Dim str As String = “
Tacoma “
str.Trim()
str.TrimStart()
str.TrimEnd()
7/20/2015
-- Tacoma
-- “Tacoma “
-- “
Tacoma”
Strings
17
Sunstring
• str.Substring(m,n)
substring consisting of n characters
beginning with the character in position m in
str
• str.Substring(m)
substring beginning with the character in
position m in str until the end of str
Dim str As String = “Tacoma”
str.Substring(0,4) is “Taco”
str.Substring(2) is “coma”
7/20/2015
Strings
18
IndexOf
• str.IndexOf(substr)
-1
if substr is a substring of str
other wise, beginning position of the first occurrence of substr in
str
Dim str As String = “Tacoma”
str.IndexOf("ati") is -1.
str.IndexOf(“co") is 2.
7/20/2015
Strings
19
IndexOf
• str.IndexOf(substr,n)
the position of the first occurrence of
substr in str in position n or greater
"fantastic".IndexOf(“a”, 3) is 4.
7/20/2015
Strings
20
The Empty String
• zero-length string ""
skips a line
"“ clear the text box
lstBox.Items.Add("")
txtBox.Text =
7/20/2015
Strings
21
Code
7/20/2015
Strings
22
Code
' Get the area code and phone number and display them
Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAnalyze.Click
Dim phoneNum As String
Dim areaCode As String
Dim num As String
Dim index As Integer
' Get the phone number entered by user
phoneNum = txtPhoneNo.Text
' Find the area code and number
phoneNum = phoneNum.Trim()
index = phoneNum.IndexOf("-")
areaCode = phoneNum.Substring(0, index)
num = phoneNum.Substring(index + 1)
' Display the area code and number
lstPhoneNo.Items.Clear()
lstPhoneNo.Items.Add(" Your area code is " & areaCode)
lstPhoneNo.Items.Add(" Your number is " & num)
End Sub
7/20/2015
Strings
23
Option Statements
• Statement is placed at the very top of
code window
• Option Explicit { On | Off}
used at file level to force explicit declaration of all
variable in that file, default value is On
• Option Strict { On | Off }
used at file level to enable or disable strict type
checking, default value is Off
7/20/2015
Strings
24
Option Explicit
• When Option Explicit is On, all
variables must be declared explicitly
using Dim, Private, Public or ReDim
7/20/2015
Strings
25
Option Strict
• Restricts implicit data type
conversions to only widening
conversions
• Provides compile-time notification of
data lost conversions
• Generates an error for any undeclared
variable
7/20/2015
Strings
26
Code
• ConvertTempCToF
7/20/2015
Strings
27
Internal Documentation
•
•
Begin the line with an apostrophe
Benefits:
• Easy to understand the program
• Easy to read long programs
because the purposes of
individual pieces can be
determined at a glance.
7/20/2015
Strings
28
Line-Continuation Character
• A long line of code can be continued
on another line by using underscore
(_) preceded by a space
msg = “Today is the day “ &
_
“ that everybody will have fun"
7/20/2015
Strings
29