String Variable, Methods and Properties VB.Net Strings String Variable, Methods and Properties
Strings String Literal and String Variable Declaration and Initialization Using String in Input and Output 4/6/2019 Strings
String String literal a sequence of characters treated as a single item, surrounded by “” String variable a name used to refer to a string 4/6/2019 Strings
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 4/6/2019 Strings
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”) 4/6/2019 Strings
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) 4/6/2019 Strings
Code 4/6/2019 Strings
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 4/6/2019 Strings
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 4/6/2019 Strings
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 4/6/2019 Strings
Code Editing code The temperature is 80F & strTempFStr & “F”) lstTemp.Items.Add( “ The temperature is “ _ & strTempFStr & “F”) Displays The temperature is 80F 4/6/2019 Strings
ANSI Character Set A numeric representation for every key on the keyboard. The numbers are ranging from 32 to 255 4/6/2019 Strings
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 4/6/2019 Strings
String Properties and Methods Option Statements Internal Documentation Strings String Properties and Methods Option Statements Internal Documentation
String Properties and Methods A string is an object, like controls, has both properties and methods. Length ToUpper Trim ToLower IndexOf Substring 4/6/2019 Strings
String Properties (Length) str.Length: the number of characters in str Dim strCity As String = “Tacoma“ strCity.Length is 6 4/6/2019 Strings
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 4/6/2019 Strings
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” 4/6/2019 Strings
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” 4/6/2019 Strings
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. 4/6/2019 Strings
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. 4/6/2019 Strings
The Empty String zero-length string "" lstBox.Items.Add("") skips a line txtBox.Text = "“ clear the text box 4/6/2019 Strings
Code 4/6/2019 Strings
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 4/6/2019 Strings
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 4/6/2019 Strings
Option Explicit When Option Explicit is On, all variables must be declared explicitly using Dim, Private, Public or ReDim 4/6/2019 Strings
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 4/6/2019 Strings
Code ConvertTempCToF 4/6/2019 Strings
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. 4/6/2019 Strings
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" 4/6/2019 Strings