Presentation is loading. Please wait.

Presentation is loading. Please wait.

13/06/20161 6.2 Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.

Similar presentations


Presentation on theme: "13/06/20161 6.2 Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do."— Presentation transcript:

1 13/06/20161 6.2 Strings Left, Right and Trim

2 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.

3 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. Dim Message As String Dim Message As String Message = “ Printers are not expensive ” Message = “ Printers are not expensive ” lblLeft.Text = “The first 5 characters are: ” lblLeft.Text = “The first 5 characters are: ” & Microsoft.VisualBasic. (Message, ) & Microsoft.VisualBasic. (Message, ) lblRight.Text = “The last 7 characters are: ” lblRight.Text = “The last 7 characters are: ” & Microsoft.VisualBasic. (Message, ) & Microsoft.VisualBasic. (Message, ) 313/06/2016 Breaking strings with Left and Right 5 Left Right 7 Print pensive Note that the Right function counts from the right side.

4 413/06/2016 Removing spaces with Trim Trim removes any spaces on the left side and right side of a string. e.g. e.g. EmployeeName = Trim(EmployeeName) txtName.Text = EmployeeName _Mr Lee_ Mr Lee

5 513/06/2016 Extension Program 1 Rewrite the “Garage” Program to use Left and Right functions instead of the Mid function. “Garage” Program“Garage” Program Note: you must write Microsoft.VisualBasic. Before the function. Note: you must write Microsoft.VisualBasic. Before the function. e.g. Microsoft.VisualBasic.Left 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 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) (Left Right Functions Version) See the next slide for some hints. See the next slide for some hints.

6 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. 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 :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.Then use this to indicate how many characters to extract from the right, in the Right function. (LEN(Duration) - (ColonPosition))(LEN(Duration) - (ColonPosition)) Note that as Right is counting from the right side and ColonPosition is given by Instr, which counts from the left side, we do not need to use ColonPosition + 1, just ColonPosition.Note that as Right is counting from the right side and ColonPosition is given by Instr, which counts from the left side, we do not need to use ColonPosition + 1, just ColonPosition. 6 13/06/2016

7 713/06/2016 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). Program 6.1 Validating a Full NameProgram 6.1 Validating a Full Name e.g. change the contents of the text box: e.g. change the contents of the text box: EmployeeName = Trim(EmployeeName) txtName.Text = EmployeeName _Mr Lee_ Mr Lee

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

9 913/06/2016 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: letters, followed by letters, followed by a single ‘*’ character, followed by a single ‘*’ character, followed by letters 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.

10 Remember – To Search: Use Mid in a loop to extract each character in turn from the beginning to end: For Index = 1 To Len(WordToBeSearched) Character = Mid(WordToBeSearched, Index, 1) If Character = “WhatYouWantToSearchFor” Then Left / Right to extract from the WordToBeSearched e.g. Microsoft.VisualBasic. Left( WordToBeSearched, Index - 1) Microsoft.VisualBasic. Right( WordToBeSearched, Len( WordToBeSearched ) - Index) Or Add the Character extracted to a separate new variable: e.g. NewWord = NewWord & Character ….. End If Next Index This may help you with the next extension programs.

11 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: input “com*put**er*”, the output is “computer” input “hardware”, the output is “hardware”

12 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, BIKE65 is a valid bike ID. Write a program that will validate the format of a bike ID input into the variable BikeID and output a suitable message.

13 1313/06/2016 Plenary What do the Left, Right, LTrim, RTrim, Trim, UCase and LCase functions do? Left returns a substring from the main string starting with the first character up to a specified length. 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. 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. LTrim removes any leading spaces at the left side of a string. RTrim removes any trailing spaces at the right side of a string. RTrim removes any trailing spaces at the right side of a string. Trim does both of the above. Trim does both of the above. UCase converts all characters in a string into their upper case equivalents. LCase vice versa above.


Download ppt "13/06/20161 6.2 Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do."

Similar presentations


Ads by Google