String Variable, Methods and Properties

Slides:



Advertisements
Similar presentations
Fundamentals of Programming in Visual Basic
Advertisements

Chapter 31 Fundamentals of Programming in Visual Basic (Continue IV) Strings Variables and Strings Using Text Boxes for Input and Output Concatenation.
Chapter 31 Fundamentals of Programming in VB(Continue I) Numbers Arithmetic Operations Variables Incrementing the Value of a Variable.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Chapter 5 new The Do…Loop Statement
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 31 Fundamentals of Programming in Visual Basic (Continue VI) String Properties and Methods: "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Created By Mayson Al-Duwais1. Using Exit to Terminate Repetition Statements To terminate different types of repetition statements you can use a special.
Chapter 3: Using Variables and Constants
CS0004: Introduction to Programming Variables – Numbers.
CS0004: Introduction to Programming Input and Output.
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
CS0004: Introduction to Programming Variables – Strings.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
Lecture 8 Visual Basic (2).
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Programming with Microsoft Visual Basic th Edition
String Manipulation 10/21/2015 Lect#6 GC Strings have their own properties and methods, just like a textbox or label or form does. 10/21/2015 Lect#6.
CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
3.4 Strings Variables and Strings Using Text Boxes for Input and Output Concatenation ANSI Character Set String Properties and Methods: LengthToUpper TrimToLower.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 VB-06-String Manipulation Mar 03, 2002 String Function VISUAL BASIC.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Data Types. Visual Basic provides data type Single for storing single-precision floating-point numbers. Data type Double requires more memory to store.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
Visual Basic Fundamental Concepts
Objective 7.04 Apply Built-in String Functions (3%)
Chapter 3 – Variables, Input, and Output
String Manipulation Reference:
Data Types, Arithmetic Operations
Variables and Arithmetic Operations
Chapter 3 – Variables, Input, and Output
Chapter 4 – Decisions 4.1 Relational and Logical Operators
Variables and Arithmetic Operations
Visual Basic..
Chapter 5 The Do…Loop Statement
Chapter 3 – Variables, Input, and Output
Fundamentals of Programming in VB.NET
Strings(Part 1) String Literal and String Variable
CIS16 Application Development Programming with Visual Basic
Section 3.3 Numbers Arithmetic Operations Variables
Variable Review & IO User 12/26/2018.
Chapter 3.5 Input and Output
Input and Output.
String Variable, Methods and Properties
Additional Topics in VB.NET
elementary programming
String Variable, Methods and Properties
Introduction to Programming
String Variable, Methods and Properties
Input and Output.
Input and Output Chapter 3.5
Presentation transcript:

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 12/1/2018 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 12/1/2018 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 12/1/2018 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”) 12/1/2018 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) 12/1/2018 Strings

Code 12/1/2018 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 12/1/2018 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 12/1/2018 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 12/1/2018 Strings

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

ANSI Character Set A numeric representation for every key on the keyboard. The numbers are ranging from 32 to 255 12/1/2018 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 12/1/2018 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 12/1/2018 Strings

String Properties (Length) str.Length: the number of characters in str Dim strCity As String = “Tacoma“ strCity.Length is 6 12/1/2018 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 12/1/2018 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” 12/1/2018 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” 12/1/2018 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. 12/1/2018 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. 12/1/2018 Strings

The Empty String zero-length string "" lstBox.Items.Add("") skips a line txtBox.Text = "“ clear the text box 12/1/2018 Strings

Code 12/1/2018 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 12/1/2018 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 12/1/2018 Strings

Option Explicit When Option Explicit is On, all variables must be declared explicitly using Dim, Private, Public or ReDim 12/1/2018 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 12/1/2018 Strings

Code ConvertTempCToF 12/1/2018 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. 12/1/2018 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" 12/1/2018 Strings