35
11/02/22 11/02/22 1 4.1 Strings 4.1 Strings ASCII ASCII & & Processing Strings with the Processing Strings with the Functions Functions - Locate - Locate (Instr) (Instr) , Mid, Length , Mid, Length (Len) (Len) , Char , Char (ChrW) (ChrW) & ASCII & ASCII (Asc) (Asc)

07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

Embed Size (px)

Citation preview

Page 1: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

04/19/2304/19/23 11

4.1 Strings4.1 Strings

ASCII ASCII

& &

Processing Strings with the Functions Processing Strings with the Functions

- Locate - Locate (Instr)(Instr), Mid, Length , Mid, Length (Len)(Len), Char , Char (ChrW) (ChrW) & ASCII & ASCII (Asc)(Asc)

Page 2: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

2204/19/2304/19/23

Learning ObjectivesLearning Objectives

Explain how relational operators (< >) Explain how relational operators (< >) compare strings.compare strings.

Explain what the Instr, Mid and Len Explain what the Instr, Mid and Len functions do.functions do.

Page 3: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

3319/04/2319/04/23

Character RepresentationCharacter Representation

Over the years different computer Over the years different computer designers have used different designers have used different sets of sets of binary codesbinary codes for representing characters for representing characters in a character set.in a character set.This has led to great difficulty in This has led to great difficulty in transferring information from one computer transferring information from one computer to another.to another. i.e. which binary code represents each i.e. which binary code represents each

charactercharacter

Page 4: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

4419/04/2319/04/23

ASCII (AASCII (American merican SStandard tandard CCode ode for for IInformation nformation IInterchange)nterchange)

Represents each character in a standard Represents each character in a standard character set as a single byte binary code.character set as a single byte binary code.The standard code form that most PCs use to The standard code form that most PCs use to allow for communication between systems. allow for communication between systems. Usually uses a 7 bit binary code so can store Usually uses a 7 bit binary code so can store 128 different characters and simple 128 different characters and simple communications protocols.communications protocols.Sufficient for all characters on a standard Sufficient for all characters on a standard keyboard plus control codes.keyboard plus control codes.

Can be extended (extended ASCII) to use 8 bits (so Can be extended (extended ASCII) to use 8 bits (so can store 256 characters) to encode Latin language can store 256 characters) to encode Latin language characters.characters.

Page 5: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

5519/04/2319/04/23

ASCIIASCII code code

The first 32 ASCII codes are used for simple The first 32 ASCII codes are used for simple communications protocols, not characters.communications protocols, not characters. e.g. ACK – acknowledge and would be sent by a e.g. ACK – acknowledge and would be sent by a

device to acknowledge receipt of data.device to acknowledge receipt of data. 0110010 – 2 0110010 – 2 0110001 – 10110001 – 1 …….... 1000001 – A1000001 – A 1000010 – B1000010 – B

Page 6: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

6619/04/2319/04/23

Representing Characters and Representing Characters and NumbersNumbers

e.g. If the ‘A’ key is pressed ‘1000001’ is e.g. If the ‘A’ key is pressed ‘1000001’ is sent to the CPU.sent to the CPU.

If the 1 key is pressed then ‘0110001’ is If the 1 key is pressed then ‘0110001’ is sent to the CPU.sent to the CPU.

If the user wants to print ‘123’ the codes If the user wants to print ‘123’ the codes for 1, 2 & 3 are sent to the printer.for 1, 2 & 3 are sent to the printer.

Page 7: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

7704/19/2304/19/23

Processing StringsProcessing Strings

Page 8: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

8804/19/2304/19/23

Using relational operatorsUsing relational operators

> or < relates to alphabetical order.> or < relates to alphabetical order. e.g. The following code will show 2 strings in a list box e.g. The following code will show 2 strings in a list box

in alphabetical order.in alphabetical order.Dim Name1 As StringDim Name1 As StringDim Name2 As StringDim Name2 As StringName1 = InputBox(“Enter a name.”)Name1 = InputBox(“Enter a name.”)Name2 = InputBox(“Enter another name.”)Name2 = InputBox(“Enter another name.”)If Name1 < Name2 ThenIf Name1 < Name2 Then

lstAlphabetical.Items.Add(Name1)lstAlphabetical.Items.Add(Name1) lstAlphabetical.Items.Add(Name2)lstAlphabetical.Items.Add(Name2)

ElseElse lstAlphabetical.Items.Add(Name2)lstAlphabetical.Items.Add(Name2) lstAlphabetical.Items.Add(Name1)lstAlphabetical.Items.Add(Name1)

End IfEnd If

Page 9: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

How ASCII codes are used to How ASCII codes are used to arrange letters in alphabetical orderarrange letters in alphabetical order

Letter a-z have increasing ASCII codes.Letter a-z have increasing ASCII codes.

Each character’s ASCII value is Each character’s ASCII value is compared.compared.

Try using the ASCII Try using the ASCII (converts a letter into its corresponding (converts a letter into its corresponding

ASCII binary code) ASCII binary code) and Char and Char (converts a ASCII binary code into (converts a ASCII binary code into

its corresponding letter) its corresponding letter) commands.commands.

9904/19/2304/19/23

Page 10: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

4.1a ASCII4.1a ASCII

Specification:Specification: Allow the user to convert a letter to its Allow the user to convert a letter to its

corresponding ASCII code and vice versa.corresponding ASCII code and vice versa.

101004/19/2304/19/23

Page 11: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

Program 4.1a ASCIIProgram 4.1a ASCII

Dim Letter As StringDim Letter As String

Dim ASCII As IntegerDim ASCII As Integer

Console.WriteLine(“Enter a letter.”)Console.WriteLine(“Enter a letter.”)

Letter = Console.ReadLineLetter = Console.ReadLine

ASCII = Asc(Letter) ASCII = Asc(Letter) ‘Convert letter to ‘Convert letter to ASCIIASCII

Console.WriteLine(“The ASCII code for ” & Console.WriteLine(“The ASCII code for ” & Letter & “is: “ ASCII)Letter & “is: “ ASCII)

111104/19/2304/19/23 Continued on the next slide.Continued on the next slide.

Page 12: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

Program 4.1a ASCIIProgram 4.1a ASCII

Console.WriteLine(“Enter a denary number.”)Console.WriteLine(“Enter a denary number.”)

ASCII = Console.ReadLineASCII = Console.ReadLine

Letter = ChrW(ASCII) Letter = ChrW(ASCII) ‘Convert ASCII code to ‘Convert ASCII code to a letter.a letter.

Console.WriteLine(“The ASCII code: ” & Console.WriteLine(“The ASCII code: ” & ASCII & “is: “ Letter)ASCII & “is: “ Letter)

121204/19/2304/19/23

Page 13: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

Program 4.1a ASCIIProgram 4.1a ASCII

Run the program and test it.Run the program and test it.

131304/19/2304/19/23

Page 14: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

The The Instr Instr functionfunction returns the position in the main returns the position in the main string of the substring being searched for.string of the substring being searched for. Assume declaration of variables below.Assume declaration of variables below. MainString = “The man looked up and saw the man in MainString = “The man looked up and saw the man in

the moon.”the moon.”

SearchString = “SearchString = “manman””

Position = Instr(MainString, SearchString)Position = Instr(MainString, SearchString)

Note that it will look only once so Instr in the example above Note that it will look only once so Instr in the example above would NOT find the second occurrence of the word “man”.would NOT find the second occurrence of the word “man”.

141404/19/2304/19/23

Searching for a substring using Searching for a substring using Locate (Locate (Instr)Instr)

55

Page 15: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

The Mid function returns a substring from the main The Mid function returns a substring from the main string.string.

Dim Character As StringDim Character As String

Dim Characters As StringDim Characters As String

MainString = “MainString = “Keep on looking ahead.Keep on looking ahead.””

Character = Mid(MainString, Character = Mid(MainString, 99, , 11))

151504/19/2304/19/23

Processing individual Processing individual characters using Midcharacters using Mid

Position Position How many characters. How many characters.

ll

Page 16: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

The Mid function returns a substring from the main The Mid function returns a substring from the main string.string.

Dim Character As StringDim Character As String

Dim Characters As StringDim Characters As String

MainString = “MainString = “Keep on looking ahead.Keep on looking ahead.””

Character = Mid(MainString, Character = Mid(MainString, 99, , 22))

161604/19/2304/19/23

Processing individual Processing individual characters using Midcharacters using Mid

Position Position How many characters. How many characters.

lolo

Page 17: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

Len returns the number of characters in a Len returns the number of characters in a string.string.

Dim EmployeeName As StringDim EmployeeName As String Dim LengthOfString As IntegerDim LengthOfString As Integer EmployeeName = “Mr Lee”EmployeeName = “Mr Lee”

LengthOfString = Len(EmployeeName)LengthOfString = Len(EmployeeName) MsgBox(“The length of the Employee’s name MsgBox(“The length of the Employee’s name

is: “ & LengthOfString)is: “ & LengthOfString)171704/19/2304/19/23

LengthLength (Len) (Len)

66

Page 18: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

String Handling FunctionsAll string handling functions give a “result”.This could be a number, character or characters.The result of a string handling function is almost always stored in a separate variable:

e.g.PositionPosition = Instr(MainString, SearchString) = Instr(MainString, SearchString)CharacterCharacter = Mid(MainString, = Mid(MainString, 99, , 11))LengthOfStringLengthOfString = Len(EmployeeName) = Len(EmployeeName)

You can look at this variable with If statements:You can look at this variable with If statements: e.g. e.g.

If Position = 2 ThenIf Position = 2 ThenIf Character = “b” ThenIf Character = “b” ThenIf LengthOfString >=5 ThenIf LengthOfString >=5 Then

You can also mix these functions:You can also mix these functions: e.g. e.g.

LengthOfStringLengthOfString = Len(MainString) = Len(MainString)Character = Mid(MainString, Character = Mid(MainString, LengthOfStringLengthOfString, , 11))If Character = “e” ThenIf Character = “e” Then

Basically asking if there is an “e” at the end of the variable MainString.Basically asking if there is an “e” at the end of the variable MainString.

Page 19: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

191904/19/2304/19/23

Program 4.1b Validating a Full NameProgram 4.1b Validating a Full Name

Specification:Specification: Ask the user to enter a person’s surname and Ask the user to enter a person’s surname and

then their first name.then their first name. Check that:Check that:

That a space has not been entered at the That a space has not been entered at the beginning of the name.beginning of the name.

Only one space character has been used between Only one space character has been used between the names. the names.

Page 20: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

202004/19/2304/19/23

Program 4.1b Validating a Full NameProgram 4.1b Validating a Full Name

Create a new project named ‘Create a new project named ‘Validating a Validating a Full NameFull Name’.’.

Page 21: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

212104/19/2304/19/23

Program 4.1b Validating a Full NameProgram 4.1b Validating a Full Name

Dim Spaces As Integer Dim Spaces As Integer

Dim FullName As StringDim FullName As String

Dim Index As IntegerDim Index As Integer

Dim Character As StringDim Character As String

Console.WriteLine(“Enter a full name.”)Console.WriteLine(“Enter a full name.”)

FullName = Console.ReadLineFullName = Console.ReadLine

Page 22: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

222204/19/2304/19/23

‘‘Is there a space at the beginning (1Is there a space at the beginning (1stst position)?position)?

If InStr(FullName, " ") = 1 ThenIf InStr(FullName, " ") = 1 Then Console.WriteLine(“You are not supposed to Console.WriteLine(“You are not supposed to

have a space at the beginning.")have a space at the beginning.") Exit SubExit Sub

End IfEnd If

Program 4.1b Validating a Full NameProgram 4.1b Validating a Full Name

Page 23: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

232304/19/2304/19/23

‘‘Len returns the length of the name. The loopLen returns the length of the name. The loop

‘‘is repeated this number of times to processis repeated this number of times to process

‘‘each character in the name.each character in the name.

For Index = 1 To Len(FullName)For Index = 1 To Len(FullName) Character = Mid(FullName, Index, 1) Character = Mid(FullName, Index, 1) ‘Extract one ‘Extract one

character.character. If Character = “ ” Then If Character = “ ” Then ‘Is this character a space?‘Is this character a space?

Spaces = Spaces + 1 Spaces = Spaces + 1 ‘If yes, increment Spaces.‘If yes, increment Spaces. End IfEnd If

Next IndexNext Index

Program 4.1b Validating a Full NameProgram 4.1b Validating a Full Name

Page 24: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

242404/19/2304/19/23

‘‘Does the name have more than one Does the name have more than one space?space?

If Spaces > 1 ThenIf Spaces > 1 Then Console.WriteLine(“Too many spaces!”)Console.WriteLine(“Too many spaces!”)

End IfEnd If

Program 4.1b Validating a Full NameProgram 4.1b Validating a Full Name

Page 25: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

252504/19/2304/19/23

Run the program and test it.Run the program and test it. Insert spaces in various different locations.Insert spaces in various different locations.

Program 4.1b Validating a Full NameProgram 4.1b Validating a Full Name

Page 26: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

Commenting on String Functions

For presentations 4.1 & 4.2 I will only ask for comments to string functions.Your comments MUST explain: What does the string function do? Why does this need to be done? When (after and before what) are you doing

this and why does it have to be done there?When in the procedure code or, if it is on its own, in which procedure (button, checkbox, textbox, etc…)?

Page 27: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

272704/19/2304/19/23

ExtendExtend the program to output an the program to output an appropriate message if a space has been appropriate message if a space has been entered after the surname.entered after the surname.

Program 4.1b Validating a Full NameProgram 4.1b Validating a Full Name

Page 28: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

Remember – To Search:

Use Mid in a loop to extract each character/s in turn from the beginning to end: For Index = 1 To Len(WordToBeSearched)

Character/s = Mid(WordToBeSearched, Index, 1)

If Character/s = “WhatYouWantToSearchFor” ThenCount = Count + 1

….. End If

Next Index

Page 29: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

292904/19/2304/19/23

Extension “Search for letter a” Extension “Search for letter a” Program 4.1cProgram 4.1c

Write a program that will count the number Write a program that will count the number of letter 'a's in a word.of letter 'a's in a word.

Extension:Extension:Adapt the program so that uppercase 'A' to be Adapt the program so that uppercase 'A' to be counted as well as each lower case 'a‘.counted as well as each lower case 'a‘.

Page 30: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

303004/19/2304/19/23

Extension “Search for text” Extension “Search for text” Program 4.1dProgram 4.1d

Write a program that allows the user to:Write a program that allows the user to: Enter some text (this could be copied and pasted in).Enter some text (this could be copied and pasted in). Enter a search letter or word Enter a search letter or word (including the word within (including the word within

other words).other words). Outputs the number of times this letter or search word Outputs the number of times this letter or search word

is found.is found.Hint: In a loop Hint: In a loop

For index = 1 To Len(For index = 1 To Len(StringToSearchStringToSearch))…… …… = Mid (StringToSearch, Index, = Mid (StringToSearch, Index, Len(TextToSearchFor))Len(TextToSearchFor))

Extension:Extension:Adapt the program to offer the user a choice of:Adapt the program to offer the user a choice of:

1. Counting whole words and words within other words, as above.1. Counting whole words and words within other words, as above.2. 2. Counting whole words Counting whole words onlyonly..

Page 31: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

313104/19/2304/19/23

Extension “Sort” Program 4.1eExtension “Sort” Program 4.1e

Write a program that allows the user to:Write a program that allows the user to: Enter two single words in two separate text Enter two single words in two separate text

boxes.boxes. When a “Sort” button is clicked the 2 words When a “Sort” button is clicked the 2 words

are added to a label with a space in between are added to a label with a space in between in alphabetical order.in alphabetical order.

Make sure you use the Mid function to Make sure you use the Mid function to examine the letters in each word, one by examine the letters in each word, one by one.one.

Make sure you also consider what happens if 2 words are the same Make sure you also consider what happens if 2 words are the same but 1 is longer but 1 is longer (e.g. apple, apples) (e.g. apple, apples) and 2 words are exactly the same.and 2 words are exactly the same.

Page 32: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

323204/19/2304/19/23

Extension “Garage” Program 4.1fExtension “Garage” Program 4.1f

A garage is having software produced to calculate the bills A garage is having software produced to calculate the bills for its customers. The garage enters details of a job.for its customers. The garage enters details of a job.

Duration Duration PartsParts

01:09 01:09 $17.07$17.07

02:52 02:52 $29.27$29.27

04:13 04:13 $43.15$43.15

The software for the garage includes a function which takes HOURS as The software for the garage includes a function which takes HOURS as a string and returns the number of half hours.a string and returns the number of half hours.For example, if the input is “1:30” the output will be 3; if the input is 2:52 For example, if the input is “1:30” the output will be 3; if the input is 2:52 the output will be 6.the output will be 6.

Continued on the next slide.Continued on the next slide.

Page 33: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

Extension “Garage” Program 4.1fExtension “Garage” Program 4.1f

Here is an algorithm for this function:Here is an algorithm for this function:

3333

Write this program.Write this program.

Please note though that you should Please note though that you should use use 1 input for the “Duration”1 input for the “Duration”, not 2, , not 2, as even though this would be simpler, it as even though this would be simpler, it avoids using the “String” functions you avoids using the “String” functions you need to practise here.need to practise here.

See the next slide for some hints.See the next slide for some hints.

Page 34: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

Extension “Garage” Program 4.1fExtension “Garage” Program 4.1f

Hints:Hints:

1.1. Look for the colon Look for the colon :: using the using the InstrInstr function to find function to find its position.its position.

2.2. Use the Use the MidMid function to extract the numbers function to extract the numbers beforebefore

thethe :: as minutes.as minutes.

3.3. Use the Use the MidMid function to extract the numbers function to extract the numbers afterafter the : as hours.the : as hours.

• Note that if you do not include a second number in the Note that if you do not include a second number in the MidMid function then function then MidMid will extract all numbers from the position will extract all numbers from the position you give, to the end.you give, to the end.

4.4. Note that if the minutes is 0 then do not use the Note that if the minutes is 0 then do not use the minutes at all minutes at all (just Hours*2)(just Hours*2)..

343404/19/2304/19/23

Page 35: 07/10/20151 4.1 Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)

353504/19/2304/19/23

PlenaryPlenary

How do relational operators (< >) compare How do relational operators (< >) compare strings?strings? > or < relates to alphabetical order.> or < relates to alphabetical order.

What do the What do the InstrInstr, , MidMid and and LenLen functions functions do?do? InstrInstr returns the position in the main string of the returns the position in the main string of the

substring being searched for.substring being searched for. Mid Mid returns a substring (from a specified location and returns a substring (from a specified location and

of a specified length) from the main string.of a specified length) from the main string. Len Len returns the number of characters in a string.returns the number of characters in a string.