Difference between revisions of "String Functions and Manipulation in VB6"
(New page: String Functions and Manipulation in VB6 Asc, Chr, Mid, Join, LSet, StrConv, StrReverse, Filter, Replace Category:Computer Technology Category:Programming Category:VB6) |
(→Null, 0, and Empty Strings) |
||
(12 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
− | String Functions and Manipulation in VB6 | + | String Functions and Manipulation in VB6 including common functions such as Asc, Chr, Mid, Join, LSet, StrConv, StrReverse, Filter, Replace |
− | + | == Functions and Description == | |
+ | === Asc (function) === | ||
+ | Returns an Integer representing the character code corresponding to the first letter in a string. | ||
+ | Syntax | ||
+ | :Asc(string) | ||
+ | === Chr (function) === | ||
+ | Returns a String containing the character associated with the specified character code. | ||
+ | |||
+ | Syntax | ||
+ | :Chr(charcode) | ||
+ | |||
+ | |||
+ | |||
+ | === InStr (function) === | ||
+ | Returns a Variant (Long) specifying the position of the first occurrence of one string within another. | ||
+ | |||
+ | Syntax | ||
+ | :InStr([start, ]string1, string2[, compare]) | ||
+ | |||
+ | Example | ||
+ | Dim SearchString, SearchChar, MyPos | ||
+ | SearchString ="XXpXXpXXPXXP" ' String to search in. | ||
+ | SearchChar = "P" ' Search for "P". | ||
+ | |||
+ | ' A textual comparison starting at position 4. Returns 6. | ||
+ | MyPos = Instr(4, SearchString, SearchChar, 1) | ||
+ | |||
+ | ' A binary comparison starting at position 1. Returns 9. | ||
+ | MyPos = Instr(1, SearchString, SearchChar, 0) | ||
+ | |||
+ | ' Comparison is binary by default (last argument is omitted). | ||
+ | MyPos = Instr(SearchString, SearchChar) ' Returns 9. | ||
+ | |||
+ | MyPos = Instr(1, SearchString, "W") ' Returns 0. | ||
+ | |||
+ | === InStrRev (function) === | ||
+ | New in VB6. | ||
+ | |||
+ | Similar to InStr but searches from end of string. Returns the position of an occurrence of one string within another, from the end of string. | ||
+ | |||
+ | Syntax | ||
+ | :InstrRev(stringcheck, stringmatch[, start[, compare]]) | ||
+ | |||
+ | === Join (function) === | ||
+ | New in VB6 | ||
+ | |||
+ | Used to join arrays elements. | ||
+ | |||
+ | === Mid (function) === | ||
+ | Returns a Variant (String) containing a specified number of characters from a string. | ||
+ | |||
+ | Syntax | ||
+ | :Mid(string, start[, length]) | ||
+ | |||
+ | === Replace (function) === | ||
+ | New in VB6. | ||
+ | |||
+ | Returns a string in which a specified substring has been replaced with another substring a specified number of times. | ||
+ | |||
+ | Syntax | ||
+ | :Replace(expression, find, replace[, start[, count[, compare]]]) | ||
+ | |||
+ | Example | ||
+ | Debug.print Replace("abababa", "a", "") | ||
+ | |||
+ | Example | ||
+ | |||
+ | public Function StripOut(From as string, What as string) as string | ||
+ | Dim i as Integer | ||
+ | StripOut = From | ||
+ | for i = 1 to len(What) | ||
+ | StripOut = Replace(StripOut, mid$(What, i, 1), "") | ||
+ | next i | ||
+ | End Function | ||
+ | |||
+ | === Reverse (function) === | ||
+ | New in VB6. | ||
+ | |||
+ | To reverse a string. | ||
+ | |||
+ | === Split (function) === | ||
+ | New in VB6 | ||
+ | |||
+ | Split a string into a variant array. | ||
+ | |||
+ | |||
+ | === StrComp (function) === | ||
+ | Returns a Variant (Integer) indicating the result of a string comparison. | ||
+ | |||
+ | Syntax | ||
+ | :StrComp(string1, string2[, compare]) | ||
+ | |||
+ | Return Values | ||
+ | If StrComp returns | ||
+ | string1 is less than string2 -1 | ||
+ | string1 is equal to string2 0 | ||
+ | string1 is greater than string2 1 | ||
+ | string1 or string2 is Null Null | ||
+ | |||
+ | == Performance Rating == | ||
+ | |||
+ | table 1 | ||
+ | |||
+ | {| class="stylish" | ||
+ | |+ Relative performance of VB6 string functions | ||
+ | |- | ||
+ | ! Function | ||
+ | ! Performance | ||
+ | ! Description | ||
+ | |- | ||
+ | | Len | ||
+ | | class="num" | 1 | ||
+ | | Len(S) returns the number of ''characters'' in string S.<br />''(Note: works differently with UDTs)'' | ||
+ | |- | ||
+ | | LenB | ||
+ | | class="num" | 1 | ||
+ | | LenB(S) returns the number of ''bytes'' in S. | ||
+ | |- | ||
+ | | AscW | ||
+ | | class="num" | 1 | ||
+ | | AscW(S) returns the ''Unicode value'' of the first character in S. | ||
+ | |- | ||
+ | | AscB | ||
+ | | class="num" | 1 | ||
+ | | AscB(S) returns the ''first byte'' in S. | ||
+ | |- | ||
+ | | Asc | ||
+ | | class="num" | 7 | ||
+ | | Asc(S) returns the ''ANSI value'' of the first character in S. | ||
+ | |- | ||
+ | | ChrW$ | ||
+ | | class="num" | 17 | ||
+ | | ChrW$(U) returns a string containing the ''Unicode character'' U. | ||
+ | |- | ||
+ | | ChrB$ | ||
+ | | class="num" | 18 | ||
+ | | ChrB$(B) returns a ''byte string'' containing the ''character'' B. | ||
+ | |- | ||
+ | | Chr$ | ||
+ | | class="num" | 24 | ||
+ | | Chr$(A) returns a string containing the ''ANSI character'' A. | ||
+ | |- | ||
+ | | Left$ | ||
+ | | class="num" | 18 | ||
+ | | Left$(S,x) returns x characters from the ''start'' of S. | ||
+ | |- | ||
+ | | Right$ | ||
+ | | class="num" | 21 | ||
+ | | Right$(S,x) returns x characters from the ''end'' of S. | ||
+ | |- | ||
+ | | Mid$ | ||
+ | | class="num" | 24 | ||
+ | | Mid$(S,x,y) returns y characters from S, starting at the ''x'th'' character. | ||
+ | |- | ||
+ | | CStr | ||
+ | | class="num" | 36 | ||
+ | | CStr(x) returns the string representation of x (localized). | ||
+ | |- | ||
+ | | Str$ | ||
+ | | class="num" | 111 | ||
+ | | Str$(x) returns the string representation of x (not localized). | ||
+ | |} | ||
+ | |||
+ | How to read the table: Calling Len takes 1 unit of time, while Asc takes 7 units. You can call Len 7 times in the same time you call Asc once. | ||
+ | |||
+ | table 2 | ||
+ | |||
+ | <TABLE CLASS="stylish"> | ||
+ | <CAPTION>Relative performance of VB6 string functions</CAPTION> | ||
+ | <TR> | ||
+ | <TH>Function</TH><TH>Performance</TH><TH>Description</TH> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>LTrim$</TD><TD CLASS="num">20</TD><TD>LTrim$(S) returns a copy of S without <EM>leading</EM> spaces.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>RTrim$</TD><TD CLASS="num">21</TD><TD>RTrim$(S) returns a copy of S without <EM>trailing</EM> spaces.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>Trim$</TD><TD CLASS="num">29</TD><TD>Trim$(S) returns a copy of S without <EM>leading or trailing</EM> spaces.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>Val</TD><TD CLASS="num">89</TD><TD>Val(S) returns the numeric value contained in S (non-localized).</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>CInt</TD><TD CLASS="num">81</TD><TD>CInt(S) returns the integer value contained in S (localized, rounded).</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>CDbl</TD><TD CLASS="num">103</TD><TD>CDbl(S) returns the double floating point value contained in S (localized).</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>LCase$</TD><TD CLASS="num">53</TD><TD>LCase$(S) returns a copy of S with <EM>lower case letters</EM>.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>UCase$</TD><TD CLASS="num">53</TD><TD>UCase$(S) returns a copy of S with <EM>upper case letters</EM>.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>StrConv(vbLowerCase)</TD><TD CLASS="num">327</TD><TD>StrConv(S,vbLowerCase) returns a copy of S with <EM>lower case letters</EM>.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>StrConv(vbUpperCase)</TD><TD CLASS="num">333</TD><TD>StrConv(S,vbUpperCase) returns a copy of S with <EM>upper case letters</EM>.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>StrComp(vbBinaryCompare)</TD><TD CLASS="num">47</TD><TD>StrComp(S1,S2,vbBinaryCompare) compares string S1 and S2 based on their <EM>Unicode values</EM>.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>StrComp(vbTextCompare)</TD><TD CLASS="num">65</TD><TD>StrComp(S1,S2,vbTextCompare) compares string S1 and S2 in a <EM>locale-dependent, case-insensitive way</EM>.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>InStr(vbBinaryCompare)</TD><TD CLASS="num">14</TD><TD>InStr(x,S1,S2,vbBinaryCompare) looks for S2 in S1, starting at position x, in a <EM>locale-independent, case-sensitive way</EM>.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD>InStr(vbTextCompare)</TD><TD CLASS="num">156</TD><TD>InStr(x,S1,S2,vbTextCompare) looks for S2 in S1, starting at position x, using <A HREF="instr.html#vbtextcompare">text comparison</A>.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD><b style="color:black;background-color:#a0ffff">InStrRev</b>(vbBinaryCompare)</TD><TD CLASS="num">69</TD><TD><b style="color:black;background-color:#a0ffff">InStrRev</b>(S1,S2,x,vbBinaryCompare) looks for S2 in S1, from position x back to 1, in a <EM>locale-independent, case-sensitive way</EM>.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD><b style="color:black;background-color:#a0ffff">InStrRev</b>(vbTextCompare)</TD><TD CLASS="num">193</TD><TD><b style="color:black;background-color:#a0ffff">InStrRev</b>(S1,S2,x,vbTextCompare) looks for S2 in S1, from position x back to 1, using <A HREF="instr.html#vbtextcompare">text comparison</A>.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD><b style="color:black;background-color:#99ff99">Replace</b>(vbBinaryCompare)</TD><TD CLASS="num">375</TD><TD><b style="color:black;background-color:#99ff99">Replace</b>(S1,S2,S3,x,y,vbBinaryCompare) replaces S2 with S3 in S1, starting at position x, max y times, in a <EM>locale-independent, case-sensitive way</EM>.</TD> | ||
+ | </TR> | ||
+ | <TR> | ||
+ | <TD><b style="color:black;background-color:#99ff99">Replace</b>(vbTextCompare)</TD><TD CLASS="num">513</TD><TD><b style="color:black;background-color:#99ff99">Replace</b>(S1,S2,S3,x,y,vbBinaryCompare) replaces S2 with S3 in S1, starting at position x, max y times, using <EM>text comparison</EM>.</TD> | ||
+ | </TR> | ||
+ | </TABLE> | ||
+ | |||
+ | Reference: http://www.aivosto.com/vbtips/stringopt2.html | ||
+ | |||
+ | | ||
+ | |||
+ | == Null, 0, and Empty Strings == | ||
+ | |||
+ | In VB, Null is a subtype of a Variant, in the same way the Empty is also. In other words, Null and Empty are both special states of a Variant variable. | ||
+ | |||
+ | vbNull and vbEmpty are the codes returned from the VarType function when applied to a value of that type, e.g. | ||
+ | |||
+ | VarType(Empty) = vbEmpty | ||
+ | VarType(Null) = vbNull | ||
+ | |||
+ | This provides one method of testing whether a value is Null or Empty. The other is by calling the IsNull and IsEmpty functions. | ||
+ | |||
+ | In a database, NULL has a similar status to Null is VB Variants, although | ||
+ | it's defined by the SQL standard rather than the VB language. | ||
+ | |||
+ | Use the NULL keyword to assign Null to a recordset field, use the IsNull function to test if a recordset fields contains Null. | ||
+ | |||
+ | * '''vbNullString''': The same as "", as in an empty string. It is more efficient to use vbNullString though. use it instead of "" | ||
+ | |||
+ | * '''vbNull''': This is supposed to be NULL but actually equals 1. | ||
+ | |||
+ | * '''vbNullChar''': | ||
+ | |||
+ | * '''Null''': Used for writing NULL, however, will appear as a 0 in a database numeric field after write. | ||
+ | |||
+ | * '''Empty''': Also appears as a 0 in a database numeric field after write. | ||
+ | |||
+ | * '''Nothing''': | ||
+ | |||
+ | === The Differences among Empty, Nothing, vbNull, vbNullChar, vbNullString and the Zero-Length String === | ||
+ | |||
+ | quoted from somewhere: | ||
+ | |||
+ | '''""''': A zero-length string (commonly called an "empty string") is technically a zero-length BSTR that actually uses six bytes of memory. In general, you should use the constant vbNullString instead, particularly when calling external DLL procedures. | ||
+ | |||
+ | '''Empty''': A variant of VarType 0 (vbEmpty) that has not yet been initialized. Test whether it is "nil" using the IsEmpty function. | ||
+ | |||
+ | '''Nothing''': Destroys an object reference using the Set statement. Test whether it is "nil" using the Is operator: | ||
+ | |||
+ | If obj Is Nothing Then... | ||
+ | |||
+ | '''Null''': A variant of VarType 1 (vbNull) that means "no valid data" and generally indicates a database field with no value. Don't confuse this with a C NULL, which indicates zero. Test whether it is "nil" using the IsNull function. | ||
+ | |||
+ | '''vbNullChar''': A character having a value of zero. It is commonly used for adding a C NULL to a string or for filling a fixed-length string with zeroes: | ||
+ | |||
+ | Path = String(255, vbNullChar) | ||
+ | |||
+ | '''vbNullString''': A string having a value of zero, such as a C NULL, that takes no memory. Use this string for calling external procedures looking for a null pointer to a string. To distinguish between vbNullString and "", use the VBA StrPtr function: StrPtr(vbNullString) is zero, while StrPtr("") is a nonzero memory address. [Note: the StrPtr() function may not be a part of the VBA members shown in the Object Browser so it will not AutoComplete. Enter the following line in the Immediate Window to determine if this function is available: ?StrPtr(vbNullString). This should return 0 immediately.] | ||
+ | |||
+ | |||
+ | | ||
+ | |||
+ | | ||
[[Category:Computer Technology]] | [[Category:Computer Technology]] | ||
[[Category:Programming]] | [[Category:Programming]] | ||
[[Category:VB6]] | [[Category:VB6]] |
Latest revision as of 16:46, 15 February 2008
String Functions and Manipulation in VB6 including common functions such as Asc, Chr, Mid, Join, LSet, StrConv, StrReverse, Filter, Replace
Functions and Description
Asc (function)
Returns an Integer representing the character code corresponding to the first letter in a string.
Syntax
- Asc(string)
Chr (function)
Returns a String containing the character associated with the specified character code.
Syntax
- Chr(charcode)
InStr (function)
Returns a Variant (Long) specifying the position of the first occurrence of one string within another.
Syntax
- InStr([start, ]string1, string2[, compare])
Example
Dim SearchString, SearchChar, MyPos SearchString ="XXpXXpXXPXXP" ' String to search in. SearchChar = "P" ' Search for "P". ' A textual comparison starting at position 4. Returns 6. MyPos = Instr(4, SearchString, SearchChar, 1) ' A binary comparison starting at position 1. Returns 9. MyPos = Instr(1, SearchString, SearchChar, 0) ' Comparison is binary by default (last argument is omitted). MyPos = Instr(SearchString, SearchChar) ' Returns 9. MyPos = Instr(1, SearchString, "W") ' Returns 0.
InStrRev (function)
New in VB6.
Similar to InStr but searches from end of string. Returns the position of an occurrence of one string within another, from the end of string.
Syntax
- InstrRev(stringcheck, stringmatch[, start[, compare]])
Join (function)
New in VB6
Used to join arrays elements.
Mid (function)
Returns a Variant (String) containing a specified number of characters from a string.
Syntax
- Mid(string, start[, length])
Replace (function)
New in VB6.
Returns a string in which a specified substring has been replaced with another substring a specified number of times.
Syntax
- Replace(expression, find, replace[, start[, count[, compare]]])
Example
Debug.print Replace("abababa", "a", "")
Example
public Function StripOut(From as string, What as string) as string Dim i as Integer StripOut = From for i = 1 to len(What) StripOut = Replace(StripOut, mid$(What, i, 1), "") next i End Function
Reverse (function)
New in VB6.
To reverse a string.
Split (function)
New in VB6
Split a string into a variant array.
StrComp (function)
Returns a Variant (Integer) indicating the result of a string comparison.
Syntax
- StrComp(string1, string2[, compare])
Return Values
If StrComp returns string1 is less than string2 -1 string1 is equal to string2 0 string1 is greater than string2 1 string1 or string2 is Null Null
Performance Rating
table 1
Function | Performance | Description |
---|---|---|
Len | 1 | Len(S) returns the number of characters in string S. (Note: works differently with UDTs) |
LenB | 1 | LenB(S) returns the number of bytes in S. |
AscW | 1 | AscW(S) returns the Unicode value of the first character in S. |
AscB | 1 | AscB(S) returns the first byte in S. |
Asc | 7 | Asc(S) returns the ANSI value of the first character in S. |
ChrW$ | 17 | ChrW$(U) returns a string containing the Unicode character U. |
ChrB$ | 18 | ChrB$(B) returns a byte string containing the character B. |
Chr$ | 24 | Chr$(A) returns a string containing the ANSI character A. |
Left$ | 18 | Left$(S,x) returns x characters from the start of S. |
Right$ | 21 | Right$(S,x) returns x characters from the end of S. |
Mid$ | 24 | Mid$(S,x,y) returns y characters from S, starting at the x'th character. |
CStr | 36 | CStr(x) returns the string representation of x (localized). |
Str$ | 111 | Str$(x) returns the string representation of x (not localized). |
How to read the table: Calling Len takes 1 unit of time, while Asc takes 7 units. You can call Len 7 times in the same time you call Asc once.
table 2
Function | Performance | Description |
---|---|---|
LTrim$ | 20 | LTrim$(S) returns a copy of S without leading spaces. |
RTrim$ | 21 | RTrim$(S) returns a copy of S without trailing spaces. |
Trim$ | 29 | Trim$(S) returns a copy of S without leading or trailing spaces. |
Val | 89 | Val(S) returns the numeric value contained in S (non-localized). |
CInt | 81 | CInt(S) returns the integer value contained in S (localized, rounded). |
CDbl | 103 | CDbl(S) returns the double floating point value contained in S (localized). |
LCase$ | 53 | LCase$(S) returns a copy of S with lower case letters. |
UCase$ | 53 | UCase$(S) returns a copy of S with upper case letters. |
StrConv(vbLowerCase) | 327 | StrConv(S,vbLowerCase) returns a copy of S with lower case letters. |
StrConv(vbUpperCase) | 333 | StrConv(S,vbUpperCase) returns a copy of S with upper case letters. |
StrComp(vbBinaryCompare) | 47 | StrComp(S1,S2,vbBinaryCompare) compares string S1 and S2 based on their Unicode values. |
StrComp(vbTextCompare) | 65 | StrComp(S1,S2,vbTextCompare) compares string S1 and S2 in a locale-dependent, case-insensitive way. |
InStr(vbBinaryCompare) | 14 | InStr(x,S1,S2,vbBinaryCompare) looks for S2 in S1, starting at position x, in a locale-independent, case-sensitive way. |
InStr(vbTextCompare) | 156 | InStr(x,S1,S2,vbTextCompare) looks for S2 in S1, starting at position x, using <A HREF="instr.html#vbtextcompare">text comparison</A>. |
InStrRev(vbBinaryCompare) | 69 | InStrRev(S1,S2,x,vbBinaryCompare) looks for S2 in S1, from position x back to 1, in a locale-independent, case-sensitive way. |
InStrRev(vbTextCompare) | 193 | InStrRev(S1,S2,x,vbTextCompare) looks for S2 in S1, from position x back to 1, using <A HREF="instr.html#vbtextcompare">text comparison</A>. |
Replace(vbBinaryCompare) | 375 | Replace(S1,S2,S3,x,y,vbBinaryCompare) replaces S2 with S3 in S1, starting at position x, max y times, in a locale-independent, case-sensitive way. |
Replace(vbTextCompare) | 513 | Replace(S1,S2,S3,x,y,vbBinaryCompare) replaces S2 with S3 in S1, starting at position x, max y times, using text comparison. |
Reference: http://www.aivosto.com/vbtips/stringopt2.html
Null, 0, and Empty Strings
In VB, Null is a subtype of a Variant, in the same way the Empty is also. In other words, Null and Empty are both special states of a Variant variable.
vbNull and vbEmpty are the codes returned from the VarType function when applied to a value of that type, e.g.
VarType(Empty) = vbEmpty VarType(Null) = vbNull
This provides one method of testing whether a value is Null or Empty. The other is by calling the IsNull and IsEmpty functions.
In a database, NULL has a similar status to Null is VB Variants, although it's defined by the SQL standard rather than the VB language.
Use the NULL keyword to assign Null to a recordset field, use the IsNull function to test if a recordset fields contains Null.
- vbNullString: The same as "", as in an empty string. It is more efficient to use vbNullString though. use it instead of ""
- vbNull: This is supposed to be NULL but actually equals 1.
- vbNullChar:
- Null: Used for writing NULL, however, will appear as a 0 in a database numeric field after write.
- Empty: Also appears as a 0 in a database numeric field after write.
- Nothing:
The Differences among Empty, Nothing, vbNull, vbNullChar, vbNullString and the Zero-Length String
quoted from somewhere:
"": A zero-length string (commonly called an "empty string") is technically a zero-length BSTR that actually uses six bytes of memory. In general, you should use the constant vbNullString instead, particularly when calling external DLL procedures.
Empty: A variant of VarType 0 (vbEmpty) that has not yet been initialized. Test whether it is "nil" using the IsEmpty function.
Nothing: Destroys an object reference using the Set statement. Test whether it is "nil" using the Is operator:
If obj Is Nothing Then...
Null: A variant of VarType 1 (vbNull) that means "no valid data" and generally indicates a database field with no value. Don't confuse this with a C NULL, which indicates zero. Test whether it is "nil" using the IsNull function.
vbNullChar: A character having a value of zero. It is commonly used for adding a C NULL to a string or for filling a fixed-length string with zeroes:
Path = String(255, vbNullChar)
vbNullString: A string having a value of zero, such as a C NULL, that takes no memory. Use this string for calling external procedures looking for a null pointer to a string. To distinguish between vbNullString and "", use the VBA StrPtr function: StrPtr(vbNullString) is zero, while StrPtr("") is a nonzero memory address. [Note: the StrPtr() function may not be a part of the VBA members shown in the Object Browser so it will not AutoComplete. Enter the following line in the Immediate Window to determine if this function is available: ?StrPtr(vbNullString). This should return 0 immediately.]