6.5 Other String Manipulation Functions Here are some other string manipulation functions which you can use: Asc(String) returns the ASCII code corresponding to the first letter in a string. For example, Asc("I love Visual Basic") is 73 since the ASCII code for "I" is 73. Chr(AsciiCode) returns the character corresponding to the ASCII code. For example, Chr(73) is "I". Asc and Chr are very helpful when the programmer wants to encode a string. For example, changing all a's to d's, b's to e's, c's to f's would require you to add 3 to the ASCII code of each character and then convert it back to a letter. Left(string, length) returns a specified number of characters from the left side of a string. For example: Dim Hello As String, NewString As String Hello = "I love Visual Basic" NewString = Left(Hello, 1) --> NewString = "I" NewString = Left(Hello, 7) --> NewString = "I love " Len(string) returns the length of the string. This is particularly helpful in For Loops that must go through each character in the string one by one. For example: For Num = 1 to Len(Hello0 LTrim(string) returns a copy of the string without leading spaces. RTrim(string) returns a copy of the string without trailing spaces. Trim(string) returns a copy of the string without leading or trailing spaces. Str(number) returns a string representation of a number. This is helpful in string manipulation of numbers. We need to convert numbers to strings in order to use all of this string manipulation functions on them. But, after string manipulating, we may need to convert them back to numbers again so that they can be added, subtracted, etc. Val(string) returns the numeric value of a string. This is often used with Str since numbers that are converted to strings may need to be converted back to numbers so that numeric evaluations can occur. StrConv(string, type_of_conversion) converts the string to upper or lower case. If the type of conversion is 1, then it puts the string into uppercase; if the type of conversion is 2, then it puts the string into lowercase; if the type of conversion is 3, it uppercases the first letter of every word in a string. String(number, character) returns a repeating character string of the length specified. Number is the length of the returned string and character specifies the character code or string expression whose first character is used to build the return string. Example: Hello = String(5, "*") -->output is ***** Hello = String(3, "Happy") -->output is HHH Activity 6.5.1 Enter the following into the Form_Load() and make note of the output Dim Word As String, LittleWord As String Dim Length, Num as Integer Word = "COMPUTER" Length = Len(Word) For Num = 1 to Length LittleWord = Mid(Word, Num, 1) Debug.Print Asc(LittleWord) Next Num Activity 6.5.2 Ex. 2 Converting Strings to Real/Integers and Vice-Versa Enter the following into the Form_Load() and make note of the output Dim Num1As String, Num2 As String Dim Num3 As Integer Num1 = "3" Num2 = "25" Num3 = 6 MsgBox "The sum is "& Val(Num1) + Val(Num2) + num3 Activity 6.5.1 Ex. 3 Finds the Sum of the Squares of the Digits that make up a Number Enter the following into the Form_Load() and make note of the output For this example, you need to create a form design that will allow the user to enter a number. Once you get the number stored in a textbox, this procedure is called to find the Sum of the Squares of the Number. This procedure is unlike the ones you have seen before because it is not connected to a particular event such as Click or KeyPress. Instead, it is called from within an event procedure or from the Form_Load. We will look at this in more detail later. For now, just enjoy the For Loop which finds the sum of the squares of the digits. For example, the sum of the squares of the digits of 56 is 61 because 5 * 5 + 6 * 6 = 25 + 36 = 61. Public Sub SumofSquares(Sum As Integer, Num As Integer) Dim Digit As Integer, Length As Integer Dim Hello As String Length = Len(Num) For Digit = 1 To Length Hello = Mid(Num, Digit, 1) Sum = Sum + (Val(Hello)) ^ 2 Next Digit End Sub