Download presentation
Presentation is loading. Please wait.
Published byΝαζωραῖος Κοτζιάς Modified over 6 years ago
1
Strings(Part 1) String Literal and String Variable
Declaration and Initialization Using String in Input and Output
2
String String literal a sequence of characters treated as a single item, surrounded by “” String variable a name used to refer to a string 12/5/2018 Strings
3
String declaration and Initialization
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 12/5/2018 Strings
4
Using Strings strVar = txtBox.Text txtBox.Text = strVar
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”) 12/5/2018 Strings
5
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 12/5/2018 Strings
6
Code 12/5/2018 Strings
7
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 12/5/2018 Strings
8
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 12/5/2018 Strings
9
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 12/5/2018 Strings
10
Code Editing code The temperature is 80F
lstTemp.Items.Add( “ The temperature is “ & tempFStr & “F”) Displays The temperature is 80F 12/5/2018 Strings
11
ANSI Character Set A numeric representation for every key on the keyboard. The numbers are ranging from 32 to 255 12/5/2018 Strings
12
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”) 32 & Chr(176) & “Fahrenheit ° Fahrenheit 12/5/2018 Strings
13
String Properties and Methods Option Statements Internal Documentation
Strings(Part 2) String Properties and Methods Option Statements Internal Documentation
14
String Properties and Methods
A string is an object, like controls, has both properties and methods. Length ToUpper Trim ToLower IndexOf Substring 12/5/2018 Strings
15
String Properties (Length)
str.Length: the number of characters in str Dim str As String = “Tacoma“ str.Length is 6 12/5/2018 Strings
16
ToUpper and ToLower str.ToUpper str.ToUpper() is TACOMA. str.ToLower
with all letters of str capitalized str.ToUpper() is TACOMA. str.ToLower with all letters of str in lowercase format str.ToLower() is tacoma 12/5/2018 Strings
17
Trim, TrimStart, TrimEnd
str.Trim with all leading and trailing spaces deleted Dim str As String = “ Tacoma “ str.Trim() Tacoma str.TrimStart() -- “Tacoma “ str.TrimEnd() “ Tacoma” 12/5/2018 Strings
18
Sunstring str.Substring(m,n) str.Substring(m)
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” 12/5/2018 Strings
19
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. 12/5/2018 Strings
20
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. 12/5/2018 Strings
21
The Empty String zero-length string ""
lstBox.Items.Add("") skips a line txtBox.Text = "“ clear the text box 12/5/2018 Strings
22
Code 12/5/2018 Strings
23
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 12/5/2018 Strings
24
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 12/5/2018 Strings
25
Option Explicit When Option Explicit is On, all variables must be declared explicitly using Dim, Private, Public or ReDim 12/5/2018 Strings
26
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 12/5/2018 Strings
27
Code ConvertTempCToF 12/5/2018 Strings
28
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. 12/5/2018 Strings
29
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" 12/5/2018 Strings
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.