1 VB-06-String Manipulation Mar 03, 2002 String Function VISUAL BASIC.

Slides:



Advertisements
Similar presentations
VB Data Validation Unit 4. Input Validation Validating user input before it is written can improve the quality of the data stored. Good validation schemes.
Advertisements

CSI 1306 PROGRAMMING IN VISUAL BASIC PART 2. Part 2  1. Strings  2. Translating Conditional Branch Instructions  3. Translation Set 2  4. Debugging.
Manipulating Strings String Functions. VB provides a large number of functions that facilitate working with strings. These are found in Microsoft.VisualBasic.Strings.
Visual Basic Statements Chapter 5. Relational Operators  OperationSymbol  Equal  =  Less than  <  Greater than  >  Not equal   Less than.
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Slide 1 VB Programming Fundamentals. Slide 2 Visual Basic Language v VB language is powerful and easy to use v Descendent of BASIC (Beginner's All-Purpose.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
Input Dialog Box An input dialog box can be used to obtain a single item of input from the user Presents a window (dialog box) requesting input Syntax:
Fundamentals of Programming in Visual Basic
VB Built-in Functions School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 5, Monday 2/10/03)
The number of calories burned per hour by cycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1pound of weight for each 3500.
Built-In Functions.
1 Pertemuan 04 Expression Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Introduction to Computing Dr. Nadeem A Khan. Lecture 14.
Introduction to Computing Dr. Nadeem A Khan. Lecture 6.
On to… string operations & functions. Concatenation (&) §When we want to combine two character strings into one new (longer) string, we can concatenate.
Chapter 5 new The Do…Loop Statement
BACS 287 Visual Basic String Manipulation. BACS 287 Visual Basic Strings In Visual Basic, a “string” is a series of text, numbers, and special characters.
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Chapter 9 Creating Formulas that Manipulate Text Microsoft Office Excel 2003.
Strings PART II STRING$ AND SPACE$. Create strings of specified number String$ creates string of specified character Space$ creates string of spaces Example:
Dani Vainstein1 VBScript Session 9. Dani Vainstein2 What we learn last session? VBScript coding conventions. Code convention usage for constants, variables,
CS0004: Introduction to Programming Input and Output.
Chapter 4: The Selection Structure
A453 Exemplar Password Program using VBA
Manipulation Masterclass By the VB Gods. In this masterclass, we will learn how to use some of the string manipulation function such as Len, Right, Left,
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Chapter Six: Working With Arrays in Visual Basic.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
Manipulating Strings. What is a string? Data composed of text characters. The data is stored in consecutive bytes of memory. Each byte stores the ASCII.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 8 - Visual Basic Schneider
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
Introduction to Programming Fundamentals of Programming in Visual Basic.
Validation final steps Stopping gaps being entered in an input.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Pay Example (PFirst98) Please use speaker notes for additional information!
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
© 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.
HNDIT Rapid Application Development
Visual Basic.net Functions. Function (Defined) A procedure that returns a value when called.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Basic GUI VISUAL BASIC. BASIC GUI Slide 2 of 53 Topic & Structure of the lesson Introduction Data Validation Use controls for making choices Write Pull.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions.
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
CSC 162 Visual Basic I Programming. String Functions LTrim( string ) –Removes leading spaces from the left side of string RTrim( string ) –Removes trailing.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
© 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. 
A variable is a name for a value stored in memory.
Making Decisions in a Program
Chapter 6 Variables What is VBScript?
Chapter 5 The Do…Loop Statement
Department Array in Visual Basic
Strings(Part 1) String Literal and String Variable
String Variable, Methods and Properties
Sub Procedures and Functions
String Variable, Methods and Properties
VBScript Session 10.
Microsoft Visual Basic 2005: Reloaded Second Edition
String Variable, Methods and Properties
String Processing 1 MIS 3406 Department of MIS Fox School of Business
String Variable, Methods and Properties
Presentation transcript:

1 VB-06-String Manipulation Mar 03, 2002 String Function VISUAL BASIC

2 VB-06-String Manipulation Mar 03, 2002 Learning Outcomes At the end of this lecture you will be able to : 1. Compare strings 2. Manipulate characters in a string using string functions 3. Reverse a string using string functions 4. Use string function to perform data validation

3 VB-06-String Manipulation Mar 03, 2002 Key Words 1. StrComp 2. Left 3. Right 4. Mid 5. Trim 6. Len 7. Ucase

4 VB-06-String Manipulation Mar 03, 2002 String Data Type n A string constant is a sequence of characters that is treated as a single item n String data type is used to store characters n Strings can be concatenated (using &) for combining strings

5 VB-06-String Manipulation Mar 03, 2002 Comparing Strings result = StrComp(txtinput1.text, txtinput2.text) Uses function StrComp to compare strings Function StrComp returns : n 0 if the strings are equal n -1 if the first string is lesser than the second string n 1 if the first string is greater than the second string

6 VB-06-String Manipulation Mar 03, 2002 Exercise n Write a program to accept two strings using two text boxes. Determine if the first string is greater than the second string. Output B is greater than A

7 VB-06-String Manipulation Mar 03, 2002 String Functions Left is used to extract a character from the left of a string Left(“Mississippi”,3) is “Mis” Right is used to extract a character from the right of a string Right(“Mississippi”,3) is “ppi” Mid is used to extract a character from a specific position in a string Mid(“Misissippi,5,1”)

8 VB-06-String Manipulation Mar 03, 2002 String Functions Ltrim -Removes leading spaces at the left side of a string. Ltrim(“_ _vb”) = “vb” Rtrim -Removes trailing spaces at the right side of a string Rtrim(“vb_ _ _”) = “vb” Trim – removes spaces on both left and right of a string Trim(“_ _ vb_ _”) = “vb”

9 VB-06-String Manipulation Mar 03, 2002 Group Exercise Write a program to do the following : a) Display the first three characters of your name b) Display the last three characters of you name c) Display the letter “P” in APIIT

10 VB-06-String Manipulation Mar 03, 2002 String Functions Len - Is used to determine the length of a string Len(“APIIT”) = 5 Ucase - Is used to convert a string to uppercase Ucase(“apiit”) is “APIIT” Lcase – Is used to convert a string to lowercase Lcase(“APIIT”) is apiit

11 VB-06-String Manipulation Mar 03, 2002 Group Exercise Show the answer for the following functions: 1. Ucase(“McD’s”) 2. Left(“ABCD”,2) 3. Mid(“shoe”,4,1) 4. Len(“shoe”) 5. Right(“snow”,3) 6. Lcase(“HELLO”)

12 VB-06-String Manipulation Mar 03, 2002 Group Exercise Write a program to count the number of vowels in you name i.e a,e,i,o,u using Mid and For..Next Loop. Display your answers to a list box. Example JOHN A = 0, E = 0, I = 0, O = 1, U =0

13 VB-06-String Manipulation Mar 03, 2002 Group Exercise n Write a program that will accept a string using a text box. Your program should display the string in reverse order. Use a text box for display. n Use the following string functions –Mid –Len –For …Next

14 VB-06-String Manipulation Mar 03, 2002 Reversing A String Reversing a string using function StrReverse txtoutput.text = StrReverse(txtinput.text)

15 VB-06-String Manipulation Mar 03, 2002 InStr Instr(“Shenandoah”, “nand”) is 4 Instr(“Just a moment”, “ ”) is 5 Instr(“Croissant”, “ist”) is 0 Is used to search whether an item is found in a string Used to return the position at which the content matches the string

16 VB-06-String Manipulation Mar 03, 2002 Using Instr for Validation Private Sub Text1_KeyPress(KeyAscii As Integer) valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN OPQRSTUVWXYZ /" If InStr(valid, Chr(KeyAscii)) = 0 Then KeyAscii = 0 End If End Sub

17 VB-06-String Manipulation Mar 03, 2002 Data Validation Program below validates a text box to accept alphabets and the ‘/’ symbol Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case Is = vbKeySpace 'control key Case Is < vbKeySpace 'control key Case vbKeyA To vbKeyZ, 97 To 122 'uppercase and lowercase Case 47 ‘ / Symbol Case Else MsgBox "Alpabets key only,please" KeyAscii = 0 End Select End Sub

18 VB-06-String Manipulation Mar 03, 2002 Exercise n Write a program to accept only numbers in a text box using the KeyAscii event. n Display an error message if any other key is pressed

19 VB-06-String Manipulation Mar 03, 2002 Homework n Write a program that will sum the digits entered into a text box. n Example n Sum of digits is 15

20 VB-06-String Manipulation Mar 03, 2002 Strings,Dates and Time VISUAL BASIC

21 VB-06-String Manipulation Mar 03, 2002 Learning Outcomes At the end of this lecture you will be able to : 1. Key Press Validation 2. Use the Val, Str and IsNumeric Function 3. Write programs using the Date and Time Functions

22 VB-06-String Manipulation Mar 03, 2002 Key Words 1. Key Press 2. IsNumeric 3. Now 4. Date 5. Time

23 VB-06-String Manipulation Mar 03, 2002 Using Key Press Event Private sub checkalphabet(keypress as integer) Select case KeyAscii Case is < vbkeyspace ‘control key. Ignore it. Case vbkeyA to vbkeyz, 97 to 122 uppercase and lowercase. Do nothing Case vbkeyspace, ASC(“,”), Asc(“.”) ‘space, comma and period. Ignore Case else msgbox “Alphabet only, please” Keyascii = 0 End select

24 VB-06-String Manipulation Mar 03, 2002 Exercise n Write a procedure to only accept alphabets in a text box. n Any other characters is not allowed, display an error message if other characters is entered.

25 VB-06-String Manipulation Mar 03, 2002 IsNumeric, Val and Str$ n Function Str$ converts numbers to strings n Function IsNumeric returns True if a string can represent a number n Function Val converts strings to numbers

26 VB-06-String Manipulation Mar 03, 2002 IsNumeric, Val and Str$ Option Explicit Private Sub cmdIsNumeric_Click() If IsNumeric(txtInput.Text) Then lblOutput.Caption = txtInput.Text & " + 10 is " & _ Str$(Val(txtInput.Text) + 10) Else lblOutput.Caption = txtInput.Text & " is not a number" End If End Sub

27 VB-06-String Manipulation Mar 03, 2002 Exercise n Write a program that will accept only digits in a text box. n Display an error message if the data entered is not numeric

28 VB-06-String Manipulation Mar 03, 2002 String Formatting FunctionValue Format( , “Standard”) 12, Format( , “Currency”)$12, Formatpercent (.734,1)73.4% Format(1/4, “Standard”)0.25 Format(-1234, “Currency”)($1,234.00) Round(123.67)124 Formatnumber( ,2) 1,234.57

29 VB-06-String Manipulation Mar 03, 2002 Exercise n Write a program to accept a number and display in currency format

30 VB-06-String Manipulation Mar 03, 2002 Date Difference Function Option Explicit ‘Display Difference between two dates Private Sub Form_Load() Call lstOutput.AddItem("Days between 1/1/98 and " & _ "12/31/98: " & DateDiff("d", "1/1/98", "12/31/98")) ‘Display Difference in Seconds between two times a = DateDiff("s", "12:00", "12:01") MsgBox a End Sub

31 VB-06-String Manipulation Mar 03, 2002 Date Format n List1.AddItem Format(Now, "dd/mm/yyyy") n List1.AddItem Format(Now, "mm/dd/yyyy") n List1.AddItem Format(Time, "HH:MM:SS AM/PM")

32 VB-06-String Manipulation Mar 03, 2002 Type Conversion Format n Function to convert string and numeric expressions to many different data types 1. CCur converts a string to currency 2. CDate converts a string to date 3. CSng converts a string to single 4. CInt converts a string to integer 5. CLng converts a string to a long value

33 VB-06-String Manipulation Mar 03, 2002 Time Functions Call lstOutput.AddItem("Time: " & Time) Call lstOutput.AddItem("Hour: " & Hour(Time)) Call lstOutput.AddItem("Minute: " & Minute(Time)) Call lstOutput.AddItem("Second: " & Second(Time))

34 VB-06-String Manipulation Mar 03, 2002 Format Date and Time n List1.additem Format(now, “mm/dd/yyyy”) 3/20/03 n List1.AddItem Format(Now, "d-mmm-yy") 20-Mar-03 n List1.AddItem Format(Now, "dddddd") Monday, March 24, 2003 n List1.additem format(now, Hh:Nn:Ss AM/PM”) 2:37:47 PM

35 VB-06-String Manipulation Mar 03, 2002 Exercise n Write a procedure to display the current date in dd/mm/yyyy format. n Write a procedure to display the current time in hh:mm

36 VB-06-String Manipulation Mar 03, 2002 Homework n Write a program that inputs a telephone number as string in the form (555) The program should extract the area code, the first three digits of the phone number and the last four digits of the phone number. The seven digits of the number should be concatenated into one string.

37 VB-06-String Manipulation Mar 03, 2002 Private Sub cmdEnter_Click() Dim s As String, phoneNumber As String Dim areaCodeValue As Long, exchange As String Dim areaCode As String, lineString As String Dim lineValue As Long Call lstDisplay.Clear s = txtInput.Text txtInput.Text = "" Call lstDisplay.AddItem("Phone: " & s) areaCode = Mid$(s, 2, 3) Call lstDisplay.AddItem("Area Code: " & areaCode) exchange = Mid$(s, 7, 3)

38 VB-06-String Manipulation Mar 03, 2002 Call lstDisplay.AddItem("Exchange: " & exchange) lineString = Right$(s, 4) Call lstDisplay.AddItem("Line: " & lineString) Call lstDisplay.AddItem("Concatenated: " & areaCode & exchange _ & lineString) areaCodeValue = CLng(areaCode) lineValue = CLng(lineString) Call lstDisplay.AddItem("Converted Area Code and Line: " & _ areaCode & " " & lineValue) End Sub

39 VB-06-String Manipulation Mar 03, 2002 Group Exercise n Write a program that accepts a users name and IC Number from text boxes. a. Accept only alphabets for name b. Accept IC Number (digits and ‘-’) only c. Convert the name to capitals d. Display the date of birth