Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 VB-06-String Manipulation Mar 03, 2002 String Function VISUAL BASIC."— Presentation transcript:

1

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

3 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

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

5 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

6 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

7 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

8 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”)

9 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”

10 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

11 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

12 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”)

13 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

14 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

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

16 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

17 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

18 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

19 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

20 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 12345 n Sum of digits is 15

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

22 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

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

24 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

25 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.

26 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

27 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

28 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

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

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

31 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

32 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")

33 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

34 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))

35 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

36 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

37 36 VB-06-String Manipulation Mar 03, 2002 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.

38 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)

39 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

40 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


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

Similar presentations


Ads by Google