Presentation is loading. Please wait.

Presentation is loading. Please wait.

02/03/20161 4.2 Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.

Similar presentations


Presentation on theme: "02/03/20161 4.2 Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do."— Presentation transcript:

1 02/03/20161 4.2 Strings Left, Right and Trim

2 202/03/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 ” Console.WriteLine = “The first 5 characters are: ” & Microsoft.VisualBasic. (Message, ) Console.WriteLine = “The first 5 characters are: ” & Microsoft.VisualBasic. (Message, ) Console.WriteLine = “The last 7 characters are: ” & Microsoft.VisualBasic. (Message, ) Console.WriteLine = “The last 7 characters are: ” & Microsoft.VisualBasic. (Message, ) Breaking strings with Left and Right 5 Left Right7 Print pensive Note that the Right function counts from the right side but returns characters from the left.

4 Combining Right & Len functions Note that as the Right function counts from the right hand side and the Instr/Mid functions count from the left hand side, you will probably need to combine the Right function with the Len function: Right(StringToCutFrom, Len(StringToCutFrom) - ….) Note though that the even though the Right functions counts characters from the right hand side, it actually returns them in left to right order.

5 502/03/2016 Removing spaces with Trim Trim removes any spaces on the left side and right side of a string. e.g. e.g. FullName = Trim(FullName) Console.WriteLine = FullName _Mr Lee_ Mr Lee

6 602/03/2016 Program 4.2a “Validating a Full Name2” Trim removes any spaces on the left and right sides of a string. Use Trim in ‘Program 4.1a 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 4.1a Validating a Full NameProgram 4.1a Validating a Full Name e.g. e.g. EmployeeName = Trim(EmployeeName) _Mr Lee_ Mr Lee

7 702/03/2016 Extension Program 4.2b Rewrite the “Garage” Program written in 4.1 to use Left and Right functions instead of the Mid function. “Garage” Program 4.1“Garage” Program 4.1 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.

8 Extension Program 4.2b – “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. 8 02/03/2016

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

10 10 Extension Program 4.2c – “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. Note: Please only use the functions discussed in this and previous presentation, as these are the ones on your syllabus (not others you may find can be used in VB).

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

12 Extension Program 4.2d – “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” Note: Please only use the functions discussed in this and previous presentation, as these are the ones on your syllabus (not others you may find can be used in VB).

13 Extension Program 4.2e – “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. Hint: If you compare strings then ASCII codes are used, so as long as you use “…”, even for numbers (e.g. “01”, “1”, “81”, etc…), then ASCII codes will be used for any comparisons.

14 1402/03/2016 Plenary What do the Left & Right functions do?

15 1502/03/2016 Plenary 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.


Download ppt "02/03/20161 4.2 Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do."

Similar presentations


Ads by Google