6'1 Strings, Dates and Time - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

6'1 Strings, Dates and Time

Description:

Dim Name1 As String. Dim Name2 As String. Name1 = InputBox('Enter a name. ... lstAlphabetical.Items.Add(Name1) End If. 6. 11/14/09. Searching for a substring ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 17
Provided by: nei566
Category:
Tags: dates | name1 | strings | time

less

Transcript and Presenter's Notes

Title: 6'1 Strings, Dates and Time


1
6.1 Strings, Dates and Time
  • ASCII Processing Strings

2
Learning Objectives
  • Explain how relational operators (lt gt) compare
    strings.
  • Explain what the Instr, Mid and Len functions do.

3
ASCII
  • American Standard Code for Information
    Interchange
  • Upper case letters (A - Z) 65 - 90.
  • Lower case letters (a z) 97 - 122.
  • Numeric digits (0 9) 48 - 57.
  • The space character is 32.

4
Processing Strings
5
Using relational operators
  • gt or lt relates to alphabetical order.
  • e.g. The following code will show 2 strings in a
    list box in alphabetical order.
  • Dim Name1 As String
  • Dim Name2 As String
  • Name1 InputBox(Enter a name.)
  • Name2 InputBox(Enter another name.)
  • If Name1 lt Name2 Then
  • lstAlphabetical.Items.Add(Name1)
  • lstAlphabetical.Items.Add(Name2)
  • Else
  • lstAlphabetical.Items.Add(Name2)
  • lstAlphabetical.Items.Add(Name1)
  • End If

6
Searching for a substring using Instr
  • The Instr function returns the position in the
    main string of the substring being searched for.
  • e.g. The following code looks for man in the
    sentence The man looked up and saw the moon. It
    will then store its position as 5 in the variable
    Position.
  • Assume declaration of variables below.
  • MainString The man looked up and saw the
    moon.
  • SearchString MAN
  • Position Instr(MainString, SearchString)
  • This is case insensitive unless you use a third
    parameter 1.

7
Processing individual characters using Mid
  • The Mid function returns a substring from the
    main string.
  • If given one number it will return the characters
    from that position to the end of the string.
  • If given two numbers it will use the first as
    above and the second one to indicate how many
    characters to return.
  • e.g. The following code will return the
    characters looking ahead. in the variable
    Characters and the character l in the variable
    Character.
  • Dim Character As String
  • Dim Characters As String
  • MainString Keep on looking ahead.
  • Characters Mid(MainString, 9)
  • Character Mid(MainString, 9, 1)

8
Returning the length of a string
  • Len returns the number of characters in a string.
  • The following code will return 6.
  • Dim EmployeeName As String
  • Dim LengthOfString As Integer
  • EmployeeName Mr Lee
  • LengthOfString Len(EmployeeName)
  • MsgBox(The length of the Employees name is
    LengthOfString)

9
Program 6.1 Validating a Full Name
  • Specification
  • Ask the user to enter a persons surname and then
    their first name into a single text box.
  • Check that only one space character has been used
    between the names.

10
Program 6.1 Validating a Full Name
  • Open a new project named Validating a Full
    Name.
  • Set the text property of the form to Validating
    a Full Name.

11
Program 6.1 Validating a Full Name
  • One label.
  • One rich text box.
  • Named rtfName.
  • One button.
  • Named butOK.

12
Program 6.1 Validating a Full Name
  • butOK code
  • Dim Spaces As Integer
  • Dim EmployeeName As String
  • Dim Index As Integer
  • Dim Character As String
  • EmployeeName rtfName.Text
  • Len returns the length of the name. The loop is
    repeated this
  • number of times to process each character in the
    name.
  • For Index 1 To Len(EmployeeName)
  • Character Mid(EmployeeName, Index, 1) Extract
    one character.
  • If Character Then Is this character a
    space?
  • Spaces Spaces 1 If yes, increment Spaces.
  • End If
  • Next Index
  • If Spaces gt 1 Then Does the name have more than
    one space?
  • MsgBox( Too many spaces!)
  • End If

13
Program 6.1 Validating a Full Name
  • Run the program and test it.
  • Insert spaces in various different locations.

14
Extension
  • Extend the program to output appropriate messages
    if one or more spaces are found
  • Before the first name.
  • After the surname.

15
Plenary
  • How do relational operators (lt gt) compare
    strings?
  • gt or lt relates to alphabetical order.
  • What do the Instr, Mid and Len functions do?
  • Instr returns the position in the main string of
    the substring being searched for.
  • Mid returns a substring (from a specified
    location and of a specified length) from the main
    string.
  • Len returns the number of characters in a string.

16
Homework
  • Write a program that allows the user to
  • Enter some text (this could be copied and pasted
    in).
  • Enter a search letter.
  • Outputs the number of times this letter occurs.
  • Extension
  • Adapt the program to count how many times a word
    is entered rather than a letter.
Write a Comment
User Comments (0)
About PowerShow.com