|
|
Please support our sponsor:
|
A
special Focus Section of Visual Basic Explorer
Searching strings
InStr([start, ]source_string, search_string[, compare_mode])
Searches for search_string inside of source_string starting at the character position start. If start is not provided, all of source_string will be searched. Compare_mode indicates how the search for search_string inside source_string is to be performed. Refer to the section Comparing Strings for more information about compare_mode.
i = InStr("This is a test", "a") 'Returns 9
i = InStr("This is a test", "is") 'Returns 3
i = InStr(4, "This is a test", "is") 'Returns 6
i = InStr("This is a test", "z") 'Returns 0 (not found)
InStrRev(source_string, search_string[, start[, compare_mode]])
NOTE: This function is new to Visual Basic, version 6.0.
This function performs the same task as InStr, only the search starts at the right end of the string and progress toward the left end.
i = InStrRev("This is a test", "a") 'Returns 9
i = InStrRev("This is a test", "is") 'Returns 6
i = InStrRev("This is a test", "is", 4) 'Returns 3
i = InStrRev("This is a test", "z") 'Returns 0 (not found)
It should be noted that the parameters to InStrRev are not in the same order as InStr.
Filter(string_array, search_string[, include[, compare_mode]])
NOTE: This function is new to Visual Basic, version 6.0.
Searches the array string_array for strings that contain search_string and returns the matching strings in an array. Include is a boolean value that indicates if matching or non-matching strings should be returned. If include is not provided, the value of True is used. Compare_mode indicates how the search for search_string is to be performed. Refer to the section Comparing Strings for more information about compare_mode.
Dim sStrings(4 to 7) As String
Dim sMatches As Variant
Dim i As Integer
sStrings(4) = "This"
sStrings(5) = "is"
sStrings(6) = "a"
sStrings(7) = "test"
sMatches = Filter(sStrings, "is")
For i = LBound(sMatches) to UBound(sMatches)
Debug.Print i; sMatches(i)
Next
'This will display the following:
' 0 "This"
' 1 "is"
Notice that the return array is zero-based (not one-based). A couple more examples follow:
sMatches = Filter(sStrings, "is", False)
'Returns:
' "a"
' "test"
sMatches = Filter(sStrings, "s")
'Returns:
' "This"
' "is"
' "test"
Miscellaneous
There are a few commands and functions which didn't seem to fit into any of the other categories...
Replace(string, search_string, replace_string[, start[, count[, compare_mode]]])
NOTE: This function is new to Visual Basic, version 6.0.
Returns a string with all occurences of search_string replaced by replace_string. Start is the first character position of string to be searched. If start is not provided, all of string is searched. A maximum of count replacements are made. If count is not provided, all possible replacements are made. Compare_mode indicates how the search for search_string is to be performed. Refer to the section Comparing Strings for more information about compare_mode.
s = "This is a test"
r = Replace(s, "is", "xx") 'Returns "Thxx xx a test"
r = Replace(s, " ", "..") 'Returns "This..is..a..test"
r = Replace(s, "is", "xx", 4) 'Returns "s xx a test"
r = Replace(s, "is", "xx", , 1) 'Returns "Thxx is a test"
r = Replace(s, " ", "") 'Returns "Thisisatest"
StrConv(string, conversion[, LCID])
Converts string based on the conversion and LCID arguments. Of all the possible values of conversion, the only "interesting" ones are: vbUpperCase (convert string to uppercase), vbLowerCase (convert string to lowercase) and vbProperCase (convert the first character of each word in string to uppercase). For a description of LCID, refer to VB's help facility.
s = "This is a test"
r = StrConv(s, vbUpperCase) 'Returns "THIS IS A TEST"
r = StrConv(s, vbLowerCase) 'Returns "this is a test"
r = StrConv(s, vbProperCase) 'Returns "This Is A Test"
LSet string_var = string
Left aligns string within the string variable string_var. The length of string_var will not change. If the value of string is shorter than string_var, spaces will be added to fill string_var.
sVar = "123456"
s = "abc"
LSet sVar = s 'sVar = "abc "
sVar = "123456"
s = "abcdefghi"
LSet sVar = s 'sVar = "abcdef"
An equivalent to LSet would be:
If Len(sVar)
sVar = s & Space$(Len(sVar) - Len(s))
Else
sVar = Left$(s)
End If
RSet string_var = string
Right aligns string within the string variable string_var. The length of string_var will not change. If the value of string is shorter than string_var, spaces will be added to fill string_var.
sVar = "123456"
s = "abc"
RSet sVar = s 'sVar = " abc"
sVar = "123456"
s = "abcdefghi"
RSet sVar = s 'sVar = "abcdef"
An equivalent to RSet would be:
If Len(sVar)
sVar = Space$(Len(sVar) - Len(s)) & s
Else
sVar = Left$(s)
End If
A possible use for RSet would be to create a column of numeric values aligned to a right-hand margin. For an example, refer to the topic Comparing strings containing numeric values in the section Tips & Tricks.
Keyword Summary
You can see a summary of the keywords we've discussed so far by clicking here.
<< Prev] [Next >>
|
|