Download presentation
Presentation is loading. Please wait.
1
String Variable, Methods and Properties
VB.Net Strings String Variable, Methods and Properties
2
Strings String Literal and String Variable
Declaration and Initialization Using String in Input and Output 12/1/2018 Strings
3
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/1/2018 Strings
4
String declaration and Initialization
Dim varName As String [= Value] 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/1/2018 Strings
5
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/1/2018 Strings
6
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) 12/1/2018 Strings
7
Code 12/1/2018 Strings
8
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 sngTempC As Single Dim sngTempF As Single Dim strTempFStr As String ' Get the temp from user sngTempC = CSng(txtTemp.Text) ' Convert tempC to TempF sngTempF = (sngTempC * 9 / 5) + 32 ' Display tempF in the list box strTempFStr = CStr(sngTempF) lstTemp.Items.Add(strTempFStr) End Sub 12/1/2018 Strings
9
The & does not add spaces
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 The & does not add spaces 12/1/2018 Strings
10
Concatenation Combining strings with numbers into a string
Dim strText As String = “My grade is “ Dim dblGrade As Double = 89 txtOutput.Text = strText & dblGrade displays My grade is 89 12/1/2018 Strings
11
Code Editing code The temperature is 80F & strTempFStr & “F”)
lstTemp.Items.Add( “ The temperature is “ _ & strTempFStr & “F”) Displays The temperature is 80F 12/1/2018 Strings
12
ANSI Character Set A numeric representation for every key on the keyboard. The numbers are ranging from 32 to 255 12/1/2018 Strings
13
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/1/2018 Strings
14
String Properties and Methods Option Statements Internal Documentation
Strings String Properties and Methods Option Statements Internal Documentation
15
String Properties and Methods
A string is an object, like controls, has both properties and methods. Length ToUpper Trim ToLower IndexOf Substring 12/1/2018 Strings
16
String Properties (Length)
str.Length: the number of characters in str Dim strCity As String = “Tacoma“ strCity.Length is 6 12/1/2018 Strings
17
ToUpper and ToLower strCity.ToUpper strCity.ToUpper() is TACOMA.
‘with all letters of str capitalized strCity.ToUpper() is TACOMA. strCity.ToLower ‘with all letters of str in lowercase format strCity.ToLower() is tacoma 12/1/2018 Strings
18
Trim, TrimStart, TrimEnd
strCity.Trim ‘with all leading and trailing spaces deleted Dim strCity As String = “ Tacoma “ strCity.Trim() Tacoma strCity.TrimStart() -- “Tacoma “ strCity.TrimEnd() “ Tacoma” 12/1/2018 Strings
19
Substring strCity.Substring(m,n) strCity.Substring(m)
substring consisting of n characters beginning with the character in position m in str strCity.Substring(m) substring beginning with the character in position m in str until the end of str Dim strCity As String = “Tacoma” strCity.Substring(0,4) is “Taco” strCity.Substring(2) is “coma” 12/1/2018 Strings
20
IndexOf strCity.IndexOf("ati") is -1. str.IndexOf(substr)
-1 if substr is a substring of str other wise, beginning position of the first occurrence of substr in str Dim strCity As String = “Tacoma” strCity.IndexOf("ati") is -1. strCity.IndexOf(“co") is 2. 12/1/2018 Strings
21
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/1/2018 Strings
22
The Empty String zero-length string ""
lstBox.Items.Add("") skips a line txtBox.Text = "“ clear the text box 12/1/2018 Strings
23
Code 12/1/2018 Strings
24
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/1/2018 Strings
25
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/1/2018 Strings
26
Option Explicit When Option Explicit is On, all variables must be declared explicitly using Dim, Private, Public or ReDim 12/1/2018 Strings
27
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/1/2018 Strings
28
Code ConvertTempCToF 12/1/2018 Strings
29
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/1/2018 Strings
30
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/1/2018 Strings
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.