VISUAL BASIC String Function VB06 String Manipulation Mar
VISUAL BASIC String Function VB-06 -String Manipulation Mar 03, 2002 1 1
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 VB-06 -String Manipulation Mar 03, 2002 2 2
Key Words 1. 2. 3. 4. 5. 6. 7. Str. Comp Left Right Mid Trim Len Ucase VB-06 -String Manipulation Mar 03, 2002 3 3
String Data Type 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 n VB-06 -String Manipulation Mar 03, 2002 4 4
Comparing Strings result = Str. Comp(txtinput 1. text, txtinput 2. text) Uses function Str. Comp to compare strings Function Str. Comp 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 VB-06 -String Manipulation Mar 03, 2002 5 5
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 VB-06 -String Manipulation Mar 03, 2002 6 6
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”) VB-06 -String Manipulation Mar 03, 2002 7 7
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” VB-06 -String Manipulation Mar 03, 2002 8 8
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 VB-06 -String Manipulation Mar 03, 2002 9 9
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 VB-06 -String Manipulation Mar 03, 2002 10 10
Group Exercise Show the answer for the following functions: 1. Ucase(“Mc. D’s”) 2. Left(“ABCD”, 2) 3. Mid(“shoe”, 4, 1) 4. Len(“shoe”) 5. Right(“snow”, 3) 6. Lcase(“HELLO”) VB-06 -String Manipulation Mar 03, 2002 11 11
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 VB-06 -String Manipulation Mar 03, 2002 12 12
Group Exercise 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 n – Mid – Len – For …Next VB-06 -String Manipulation Mar 03, 2002 13 13
Reversing A String Reversing a string using function Str. Reverse txtoutput. text = Str. Reverse(txtinput. text) VB-06 -String Manipulation Mar 03, 2002 14 14
In. Str Is used to search whether an item is found in a string Used to return the position at which the content matches the string Instr(“Shenandoah”, “nand”) is 4 Instr(“Just a moment”, “ ”) is 5 Instr(“Croissant”, “ist”) is 0 VB-06 -String Manipulation Mar 03, 2002 15 15
Using Instr for Validation Private Sub Text 1_Key. Press(Key. Ascii As Integer) valid = "abcdefghijklmnopqrstuvwxyz. ABCDEFGHIJKLMN OPQRSTUVWXYZ /" If In. Str(valid, Chr(Key. Ascii)) = 0 Then Key. Ascii = 0 End If End Sub VB-06 -String Manipulation Mar 03, 2002 16 16
Data Validation Program below validates a text box to accept alphabets and the ‘/’ symbol Private Sub Text 1_Key. Press(Key. Ascii As Integer) Select Case Key. Ascii Case Is = vb. Key. Space 'control key Case Is < vb. Key. Space 'control key Case vb. Key. A To vb. Key. Z, 97 To 122 'uppercase and lowercase Case 47 ‘ / Symbol Case Else Msg. Box "Alpabets key only, please" Key. Ascii = 0 End Select End Sub VB-06 -String Manipulation Mar 03, 2002 17 17
Exercise n Write a program to accept only numbers in a text box using the Key. Ascii event. n Display an error message if any other key is pressed VB-06 -String Manipulation Mar 03, 2002 18 18
Homework Write a program that will sum the digits entered into a text box. n Example 12345 n Sum of digits is 15 n VB-06 -String Manipulation Mar 03, 2002 19 19
VISUAL BASIC Strings, Dates and Time VB-06 -String Manipulation Mar 03, 2002 20 20
Learning Outcomes At the end of this lecture you will be able to : 1. Key Press Validation 2. Use the Val, Str and Is. Numeric Function 3. Write programs using the Date and Time Functions VB-06 -String Manipulation Mar 03, 2002 21 21
Key Words 1. 2. 3. 4. 5. Key Press Is. Numeric Now Date Time VB-06 -String Manipulation Mar 03, 2002 22 22
Using Key Press Event Private sub checkalphabet(keypress as integer) Select case Key. Ascii Case is < vbkeyspace ‘control key. Ignore it. Case vbkey. A 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 VB-06 -String Manipulation Mar 03, 2002 23 23
Exercise 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. n VB-06 -String Manipulation Mar 03, 2002 24 24
Is. Numeric , Val and Str$ n Function Str$ converts numbers to strings n Function Is. Numeric returns True if a string can represent a number n Function Val converts strings to numbers VB-06 -String Manipulation Mar 03, 2002 25 25
Is. Numeric, Val and Str$ Option Explicit Private Sub cmd. Is. Numeric_Click() If Is. Numeric(txt. Input. Text) Then lbl. Output. Caption = txt. Input. Text & " + 10 is " & _ Str$(Val(txt. Input. Text) + 10) Else lbl. Output. Caption = txt. Input. Text & " is not a number" End If End Sub VB-06 -String Manipulation Mar 03, 2002 26 26
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 VB-06 -String Manipulation Mar 03, 2002 27 27
String Formatting Function Format(12345. 628, “Standard”) Format(12345. 628, “Currency”) Formatpercent (. 734, 1) Format(1/4, “Standard”) Format(-1234, “Currency”) Round(123. 67) Formatnumber(1234. 567, 2) VB-06 -String Manipulation Mar 03, 2002 Value 12, 345. 63 $12, 345. 63 73. 4% 0. 25 ($1, 234. 00) 124 1, 234. 57 28 28
Exercise n Write a program to accept a number and display in currency format VB-06 -String Manipulation Mar 03, 2002 29 29
Date Difference Function Option Explicit ‘Display Difference between two dates Private Sub Form_Load() Call lst. Output. Add. Item("Days between 1/1/98 and " & _ "12/31/98: " & Date. Diff("d", "1/1/98", "12/31/98")) ‘Display Difference in Seconds between two times a = Date. Diff("s", "12: 00", "12: 01") Msg. Box a End Sub VB-06 -String Manipulation Mar 03, 2002 30 30
Date Format List 1. Add. Item Format(Now, "dd/mm/yyyy") n List 1. Add. Item Format(Now, "mm/dd/yyyy") n List 1. Add. Item Format(Time, "HH: MM: SS AM/PM") n VB-06 -String Manipulation Mar 03, 2002 31 31
Type Conversion Format n Function to convert string and numeric expressions to many different data types 1. CCur converts a string to currency CDate converts a string to date CSng converts a string to single CInt converts a string to integer CLng converts a string to a long value 2. 3. 4. 5. VB-06 -String Manipulation Mar 03, 2002 32 32
Time Functions Call lst. Output. Add. Item("Time: " & Time) Call lst. Output. Add. Item("Hour: " & Hour(Time)) Call lst. Output. Add. Item("Minute: " & Minute(Time)) Call lst. Output. Add. Item("Second: " & Second(Time)) VB-06 -String Manipulation Mar 03, 2002 33 33
Format Date and Time n n List 1. additem Format(now, “mm/dd/yyyy”) 3/20/03 List 1. Add. Item Format(Now, "d-mmm-yy") 20 -Mar-03 List 1. Add. Item Format(Now, "dddddd") Monday, March 24, 2003 List 1. additem format(now, Hh: Nn: Ss AM/PM”) 2: 37: 47 PM VB-06 -String Manipulation Mar 03, 2002 34 34
Exercise 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 n VB-06 -String Manipulation Mar 03, 2002 35 35
Homework n Write a program that inputs a telephone number as string in the form (555) 555 -5555. 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. VB-06 -String Manipulation Mar 03, 2002 36 36
Private Sub cmd. Enter_Click() Dim s As String, phone. Number As String Dim area. Code. Value As Long, exchange As String Dim area. Code As String, line. String As String Dim line. Value As Long Call lst. Display. Clear s = txt. Input. Text = "" Call lst. Display. Add. Item("Phone: " & s) area. Code = Mid$(s, 2, 3) Call lst. Display. Add. Item("Area Code: " & area. Code) exchange = Mid$(s, 7, 3) VB-06 -String Manipulation Mar 03, 2002 37 37
Call lst. Display. Add. Item("Exchange: " & exchange) line. String = Right$(s, 4) Call lst. Display. Add. Item("Line: " & line. String) Call lst. Display. Add. Item("Concatenated: " & area. Code & exchange _ & line. String) area. Code. Value = CLng(area. Code) line. Value = CLng(line. String) Call lst. Display. Add. Item("Converted Area Code and Line: " & _ area. Code & " " & line. Value) End Sub VB-06 -String Manipulation Mar 03, 2002 38 38
Group Exercise n a. b. c. d. Write a program that accepts a users name and IC Number from text boxes. Accept only alphabets for name Accept IC Number (digits and ‘-’) only Convert the name to capitals Display the date of birth VB-06 -String Manipulation Mar 03, 2002 39 39
- Slides: 39