Here are some commonly used VB string functions:
Function | Description | Example | Result |
Left(str, numchar) | Extracts the specified number of characters from the left side of the string str. | mystr = Left("Hello World", 5) | "Hello" |
Right(str, numchar) | Extracts the specified number of characters from the right side of the string str. | mystr = Right("Hello World", 5) | "World" |
Mid(str, startchar, [numchar]) | Extracts a string of length numchar from the middle of str, starting at startchar. If numchar is omitted, the entire right-hand portion of the string, beginning at startchar, is extracted. | mystr = Mid("Hello World", 7, 1) |
"W" |
Len(str) | Returns the length of str. | n = Len("Hello World") | 11 |
UCase(str) | Converts the string to all uppercase characters. | mystr = UCase("Hello World") | "HELLO WORLD" |
LCase(str) | Converts the string to all lowercase characters. | mystr = LCase("Hello World") | "hello world" |
LTrim(str) | Returns a copy of the string without leading spaces | mystr = LTrim(" Hello ") | "Hello " |
RTrim(str) | Returns a copy of the string without trailing spaces | mystr = RTrim(" Hello ") | " Hello" |
Trim(str) | Returns a copy of the string without leading or trailing spaces | mystr = Trim(" Hello ") | "Hello" |
StrReverse(str) | Returns a copy of the string in reverse order | mystr = StrReverse("Hello World") | "dlroW olleH" |
Replace(expression, find, replace, [start], [count], [compare]) | Replaces each instance of “find” with “replace” in “expression”. | mystr = Replace("Hello World, "Hello", "Goodbye") mystr = Replace("Hello World, "l", "") mystr = Replace("Hello World, "o", "", 1, 1) mystr = Replace("Hello World, "o", "", 1) mystr = Replace("Hello World, "l", "", 6) |
"Goodbye World" "Heo Word" "Hell World" "Hell Wrld" "Hello Word" |
InStr([start], string1, string2, [compare]) | Returns an integer representing the position of string2 inside string1. Start is an optional starting location (if omitted, search starts at position 1). If string2 is not found in string1, the function returns zero. | n = InStr(1, "Hello World", "W") n = InStr(1, "Hello World", "N") |
7 0 |
StrConv(string, conversion, [LCID]) | Returns a copy of string after modifying the string based on the conversion argument. The conversion argument should be a vb constant with options including vbUpperCase, vbLowerCase, vbProperCase. | mystr = StrConv("Hello World", vbUpperCase) mystr = StrConv("Hello World", vbLowerCase) mystr = StrConv("hello world", vbProperCase) |
HELLO WORLD hello world Hello World |