6 2 Strings Left Right and Trim 132022

  • Slides: 13
Download presentation
6. 2 Strings Left, Right and Trim 1/3/2022 1

6. 2 Strings Left, Right and Trim 1/3/2022 1

Learning Objectives Explain what the Left, Right and Trim functions do. 1/3/2022 2

Learning Objectives Explain what the Left, Right and Trim functions do. 1/3/2022 2

Breaking strings with Left and Right Left returns a substring from the main string

Breaking strings with Left and Right Left returns a substring from the main string starting with the first character up to a specified length. Right returns a substring from the main string starting with last character up to a specified length. n Dim Message As String n Message = “Printers are not expensive” n n 1/3/2022 lbl. Left. Text = “The first 5 characters are: ” & Microsoft. Visual. Basic. Left (Message, 5 ) Print pensive lbl. Right. Text = “The last 7 characters are: ” pensive & Microsoft. Visual. Basic. Right (Message, 7 ) Note that the Right function counts from the right side. 3

Removing spaces with Trim removes any spaces on the left side and right side

Removing spaces with Trim removes any spaces on the left side and right side of a string. n e. g. Employee. Name = Trim(Employee. Name) txt. Name. Text = Employee. Name _Mr Lee_ Mr Lee 1/3/2022 4

Extension Program 1 Rewrite the “Garage” Program to use Left and Right functions instead

Extension Program 1 Rewrite the “Garage” Program to use Left and Right functions instead of the Mid function. n Note: you must write Microsoft. Visual. Basic. Before the function. e. g. Microsoft. Visual. Basic. Left n 1/3/2022 Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (Left Right Functions Version) See the next slide for some hints. 5

Extension Program 1 – “Garage” with Left & Right functions Hints: 1. Look for

Extension Program 1 – “Garage” with Left & Right functions Hints: 1. Look for the colon : using the Instr function to find its position. 2. Use the Left function to extract the numbers before the : as hours. 3. Use the Right function to extract the numbers after the : as minutes. 1/3/2022 • Note that as the Instr function counts from the left side and the Right function counts from the right side, you will need to use the Len function to measure the length of the Duration and then subtract the position of the colon : • Then use this to indicate how many characters to extract from the right, in the Right function. • (LEN(Duration) - (Colon. Position)) • Note that as Right is counting from the right side and Colon. Position is given by Instr, which counts from the left side, we do not need to use Colon. Position + 1, just Colon. Position. 6

Extension Program 2 Trim removes any spaces on the left side and right side

Extension Program 2 Trim removes any spaces on the left side and right side of a string. Use Trim in ‘Program 6. 1 Validating a Full Name’ to remove any accidental spaces before the surname and after the first name (after each of the two appropriate message boxes). n e. g. change the contents of the text box: Employee. Name = Trim(Employee. Name) txt. Name. Text = Employee. Name _Mr Lee_ 1/3/2022 Mr Lee 7

Concatenation Joins strings together using the & operator. n e. g. Putting a variable

Concatenation Joins strings together using the & operator. n e. g. Putting a variable in a message: Msg. Box (“My name is “ & Name & “. I am “ & Age & “ years old. ”) Will show My name is …… I am … years old. 1/3/2022 This may help you with the next extension program. 8

Extension Program 3 – “Concatenation” Gina is developing her programming skills in string handling.

Extension Program 3 – “Concatenation” Gina is developing her programming skills in string handling. She is going to input two strings. Each string is made up of three parts: n n n letters, followed by a single ‘*’ character, followed by letters The groups of letters after the ‘*’ characters are joined together to form a new string which is then output. For example, with “DFG*COM” and “B*PUTER” as inputs, the new string output will be “COMPUTER”. Write the program to perform this task. 1/3/2022 9

Remember – To Search: Use Mid in a loop to extract each character in

Remember – To Search: Use Mid in a loop to extract each character in turn from the beginning to end: n For Index = 1 To Len(Word. To. Be. Searched) Character = Mid(Word. To. Be. Searched, Index, 1) If Character = “What. You. Want. To. Search. For” Then Left / Right to extract from the Word. To. Be. Searched e. g. Left(Word. To. Be. Searched, Index - 1) Microsoft. Visual. Basic. Right(Word. To. Be. Searched, Len(Word. To. Be. Searched) - Index) Microsoft. Visual. Basic. n Or Add the Character extracted to a separate new variable: e. g. New. Word = New. Word & Character …. . n End If Next Index This may help you with the next extension programs.

Extension Program 4 – “Remove *” Gina is developing her programming skills in string

Extension Program 4 – “Remove *” Gina is developing her programming skills in string handling. She is going to input one string. The string contains letters and zero or more '*' characters in any positions. Gina wants to remove all the '*'s and output the letters in their original order. For example: n n input “com*put**er*”, the output is “computer” input “hardware”, the output is “hardware”

Extension Program 5 – “BIKE IDs” All bikes in stock have a bike ID.

Extension Program 5 – “BIKE IDs” All bikes in stock have a bike ID. Each bike ID is six characters long and has the format BIKEXX, where X is a digit. For example, BIKE 65 is a valid bike ID. Write a program that will validate the format of a bike ID input into the variable Bike. ID and output a suitable message.

Plenary What do the Left, Right, LTrim, RTrim, UCase and LCase functions do? n

Plenary What do the Left, Right, LTrim, RTrim, UCase and LCase functions do? n n n 1/3/2022 Left returns a substring from the main string starting with the first character up to a specified length. Right returns a substring from the main string starting with last character up to a specified length. LTrim removes any leading spaces at the left side of a string. RTrim removes any trailing spaces at the right side of a string. Trim does both of the above. UCase converts all characters in a string into their upper case equivalents. LCase vice versa above. 13