STRING FUNCTION Learning Outcomes At the end of this lecture you will be able to : 1.Compare strings 2.Manipulate characters in a string using string functions 3.Reverse a string using string functions 4.Use Instr string function to perform data validation Key Words 1. 2. 3. 4. 5. 6. 7. 8. StrComp Left Right Mid Trim Len Ucase Instr String Data Type ■ A string constant is a sequence of characters that is treated as a single item ■ String data type is used to store characters ■ Strings can be concatenated (using &) for combining strings Comparing Strings Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click lblresult.text = StrComp(txtinput1.text, txtinput2.text) End Sub Uses function StrComp to compare strings Function StrComp returns : 0 if the strings are equal -1 if the first string is lesser than the second string 1 if the first string is greater than the second string EXERCISE (Comparing Strings) ■ Write a program to accept two strings using two text boxes. Determine if the first string is greater than the second string. Output B is greater than A String Functions ■ Strings.Left is used to extract a character from the left of a string. – Syntax: Strings.Left(String, Length) Example: Label1.Text = Strings.Left("Mississippi", 3) OUTPUT: Mis ■ Strings.Right is used to extract a character from the right of a string. – Syntax: Strings.Right(String, Length) Example: Label1.Text = Strings.Right("Mississippi", 3) OUTPUT: ppi ■ Strings.Mid is used to extract a character from a specific position in a string. – Syntax: Strings.Mid(String, Start, Length) Example: Label1.Text = Strings.Mid("Mississippi", 5,2) OUTPUT: is ■ Strings.Left is used to extract a character from the left of a string Syntax: Strings.Left(String, Length) Example: Label1.Text = Strings.Left(TextBox1.Text, 5) OUTPUT : Hello – ■ Strings.Right is used to extract a character from the right of a string Syntax: Strings.Right(String, Length) Example: Label1.Text = Strings.Right(TextBox1.Text, 5) OUTPUT : World – ■ Strings.Mid is used to extract a character from a specific position in a string Syntax: Strings.Mid(String, Start, Length) Example: Label1.Text = Strings.Mid(TextBox1.Text, 3, 5) OUTPUT: llo W – String Functions ■ Ltrim -Removes leading spaces at the left side of a string. – Syntax: LTrim(String) Example: Ltrim(“_ _vb”) OUTPUT: “vb” ■ Rtrim -Removes trailing spaces at the right side of a string. – Syntax: RTrim(String) Example: Rtrim(“vb_ _ _”) OUTPUT: “vb” ■ Trim – removes spaces on both left and right of a string. – Syntax: LTrim(String) Example: Trim(“_ _ vb_ _”) OUTPUT: “vb” ■ Ltrim -Removes leading spaces at the left side of a string. – Syntax: LTrim(String) Example: Label1.Text = LTrim(TextBox1.Text) OUTPUT: “Hello World ” ■ Rtrim -Removes trailing spaces at the right side of a string. – Syntax: RTrim(String) Example: Label1.Text = RTrim(TextBox1.Text) OUTPUT: “ Hello World” ■ Trim – removes spaces on both left and right of a string. – Syntax: Trim(String) Example: Label1.Text = Trim(TextBox1.Text) OUTPUT: “Hello World” EXERCISE (String Functions) Write a program to do the following : a) Display the first three characters of your name b) Display the last three characters of you name c) Display the letter “S” in BSIT String Functions ■ Strings.Len - Is used to determine the length of a string. – Syntax: Strings.Len(String) Example: Label1.text = Strings.Len(“Welcome”) OUTPUT: 7 ■ Strings.Ucase - Is used to convert a string to uppercase. – Syntax: Strings.Ucase(String) Example: Label1.text = Strings.Ucase(“Welcome”) OUTPUT: WELCOME ■ Strings.Lcase – Is used to convert a string to lowercase – Syntax: Strings.Lcase(String) Example: Label1.text = Strings.Lcase(“Welcome”) OUTPUT: welcome ■ Strings.Len - Is used to determine the length of a string. – Syntax: Strings.Len(String) Example: Label1.text = Strings.Len(TextBox1.Text) OUTPUT: 11 ■ Strings.Ucase - Is used to convert a string to uppercase. – Syntax: Strings.Ucase(String) Example: Label1.text = Strings.Ucase(TextBox1.Text) OUTPUT: HELLO WORLD ■ Strings.Lcase – Is used to convert a string to lowercase – Syntax: Strings.Lcase(String) Example: Label1.text = Strings.Lcase(TextBox1.Text) OUTPUT: hello world Exercise Show the answer for the following functions: 1. Strings.Ucase(“McD’s”) 2. Strings.Left(“Pinterest”,3) 3. Strings.Mid(“Facebook”,5,4) 4. Strings.Len(“Netflix”) 5. Strings.Right(“Pinterest”,8) 6. Strings.Lcase(“TikTok”) Exercise Write a program that will accept a string using a text box. Your program should display the string in reverse order. Use a text box for display. Use the following string functions Mid Len For …Next Reversing A String ■ Reversing a string using function StrReverse. – Syntax: StrReverse(String) Example: Label1.text = StrReverse(“Welcome”) OUTPUT: emocleW Reversing A String Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click TextBox2.Text = StrReverse(TextBox1.Text) End Sub OUTPUT: InStr ■ Is used to search whether an item is found in a string ■ Used to return the position at which the content matches the string. – Syntax: Instr(String) Example: Label1.text = Instr(“Alcohol”, “o”) OUTPUT: 4 Example: Label1.text = (“Just a moment”, “ ”) OUTPUT: 5 Example: Label1.text = Instr(“Croissant”, “ist”) OUTPUT: 0 InStr The InStr( ) method of string variables tells you what the position of one string is inside another. For example, if your string was "me@me.com" and you wanted to know if the string contained the @ symbol, you could use InStr( ) Method. You would use it like this FirstString = "me@me.com" SecondString = "@" position = InStr( FirstString, SecondString ) InStr The variable FirstString is the string we want to search; SecondString is what we want to search for. You can specify a starting position for the search to begin. If you do, this number goes at the start (the default is zero): position = InStr( 1, FirstString, SecondString ) The variable called position has to be an integer variable. That's because the InStr() Method returns a number, and not text. In the code above, position would have a value of 3. That's because the @ symbols starts at the third letter of "me@me.com". *(Note: the InStr() Method starts counting at 1, and not zero like Chars(), which is very confusing!) InStr If the string you're searching for is not found, then the value placed inside of your integer variable (position in our case) is zero. That enables you to code something like this: Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Label1.Text = InStr(TextBox1.Text, TextBox2.Text) If Label1.Text = 0 Then MessageBox.Show("Not a Valid email address: There was No @ Sign") End If End Sub OUTPUT OUTPUT ARIGATHANKS!!! ANA GADON