A sub-string is a string that is created or retrieved from an existing string. Once again, the Microsoft Visual Basic library provides different techniques of creating it. These include getting characters from the left, from the right, or from any part inside the string. The Left() function can be used to get a number of characters from the left side of a string. The syntax of this function is: Left(string, length) As String This function takes two required arguments. The first argument is the string on which the operation will be performed. The second argument, a constant integer, is the number of characters to retrieve from the first argument. Here is an example: Private Sub cmdCreate_Click() Dim strValue As String Dim strLeft As String strValue = "République d'Afrique du Sud" strLeft = Left(strValue, 10) MsgBox strValue MsgBox strLeft End Sub This would produce: Because the Left() function produces a string, you can also write it as Left$ with the $ sign indicating that it returns a string. As its name suggests, the Left() function creates a new string using characters on the left side of the considered string. If you want your sub-string to contain characters from the right side of a string, you can call the Right() function. Its syntax is: Right(string, length) As String This function follows the same rules as the Left() function except that it works from the right side. Here is an example: Private Sub cmdCreate_Click() Dim strValue As String Dim strRight As String strValue = "République d'Afrique du Sud" strRight = Right(strValue, 14) MsgBox strValue MsgBox strRight End Sub This would produce: As mentioned for the Left() function, you can write the Right() function as Right$ to indicate that it returns a string. While the Left$() and the Right$() functions work on both sides of a string, you may want to use characters starting at any position of your choice to create a sub-string. This operation is supported by the Mid() function. Its syntax is: Mid(string, start[, length) As String This function takes two required and one optional arguments. The first argument, which is required, is the string on which the operation will be carried. The second argument, also required, is the position from where to start the sub-string inside the string argument. Here is an example: Private Sub cmdCreate_Click() Dim strValue As String Dim strMid As String strValue = "République d'Afrique du Sud" strMid = Mid(strValue, 14) MsgBox strValue MsgBox strMid End Sub This would produce: As you can see, if you omit the third argument, the returning sub-string would start at the start position from the string argument up to the end of the string. If you prefer, you can create a sub-string that stops before the end. To do this, you can pass the number of characters as the third argument. Here is an example: Private Sub cmdCreate_Click() Dim strValue As String Dim strMid As String strValue = "République d'Afrique du Sud" strMid = Mid(strValue, 14, 7) MsgBox strValue MsgBox strMid End Sub This would produce:
If you have a string and want to find a character or a sub-string in it, you can call the InStr() function. Its syntax is: InStr([start, ]string1, string2[, compare]) The first argument specifies the position from the string where to start looking for. This first argument is not required. The second argument is required and specifies the string to examine. The third argument specifies the character or the string to look for in the second argument. The fourth argument, which is optional, specifies whether the criterion would be binary or text-based. Here is an example: Private Sub cmdCreate_Click() Dim strValue As String Dim iPos As Integer strValue = "Republic of South Africa" iPos = InStr(1, strValue, "South") MsgBox "In """ & strValue & """, " & " South can be found at " & CStr(iPos) End Sub This would produce:
The InStr() function works from the left side of the considered string. If you want to find an occurrence of one or more characters from the right side side of a string, you can use the InStrRev() function instead.
After finding a character or a sub-string in an existing string, one of the operations you can perform would consist of replacing that character or that sub-string with another character or a sub-string. To do this, you can call the Replace() function. Its syntax is: Replace(expression, find, replace[, start[, count[, compare]]]) The first argument to this function, expression, is required. It holds the string that will be considered. The second argument, find, also required, is the character or the sub-string to look for in the expression. The third argument also is required. It also is passed as a string. If the find string is found in expression, it would be replaced with the replace string. When you call the Replace() function with the three required arguments, it would proceed from the most left character of the expression string. Instead of start with the first character, you can specify another starting position of your choice within expression. The fourth argument, which is optional, allows you to pass this factor as a constant integer. The fifth argument also is optional. If you omit it, every time find would be found in expression, it would be replaced with replace, as many times as possible. If you want, you can limit the number of times this replacement should occur even if find is found more than once. To specify this factor, pass the fifth argument as a constant integer. The compare argument, also optional, allows you specify whether the comparison would be carried in text or binary format. This argument can be passed as Text or Binary. |
|
|||||||||||
|