|
CamelCase - Convert a string to camel case
|
Total Hit (9026) |
«Code LangId=1»
' convert a string to camel case
' for example: CamelCase("new file name") => "NewFileName"
'
Function CamelCase(ByVal Text As String) As String
Dim i As Long
' convert all non-alphanumeric chars to spaces
For i = Len(Text) To 1 Step -1
If InStr(1,
....Read More |
Rating
|
|
|
Always use "$-typed" string functions
|
Total Hit (2876) |
VB official documentation seems to encourage you to use "$-less" string functions, e.g. Left, LTrim or UCase, instead of their time-honored Left$, LTrim$ and UCase$ counterparts. If you do so, however, you should be aware that the former ones return a variant value that, when used within string expr
....Read More |
Rating
|
|
|
Avoid append operations with the Replace function
|
Total Hit (2947) |
I find the Replace function very useful when I want to avoid too many append operations just to insert non-printable characters. For example, the following statement:
«Code LangId=1»
MsgBox "Disk not ready." & vbCr & vbCr & _
"Please check that the diskette is in the drive" & vbCr & _
....Read More |
Rating
|
|
|
Count distinct characters in a string
|
Total Hit (3889) |
When you need to count how many occurrences of a given character are in a string, you might be tempted to go along the "traditional" Visual Basic way:
«Code LangId=1»
' count spaces
For i = 1 To Len(text)
If Mid$(text, i, 1) = " " Then count = count + 1
Next
«/Code»
Ev
....Read More |
Rating
|
|
|
Faster string appending with Mid$ command
|
Total Hit (3247) |
As you probably know, the "&" operator is rather slow, especially with long strings. When you have to repeatedly append chucks of characters to the same variable, you can speed up your code using a simple trick based on the Mid$ command. The idea is that you pre-allocate a buffer long enough to acco
....Read More |
Rating
|
|
|
Fixed-length strings allocate and deallocate faster
|
Total Hit (2796) |
Fixed-length strings are generally slower than conventional strings, because all VB string functions and operators only recognize conventional strings, thus all fixed-length strings must be transparently converted to and from conventional strings.
However, arrays of fixed-length strings occupy a
....Read More |
Rating
|
|
|
Fixed-length strings allocate and deallocate faster
|
Total Hit (2963) |
Fixed-length strings are generally slower than conventional strings, because all VB string functions and operators only recognize conventional strings, thus all fixed-length strings must be transparently converted to and from conventional strings.
However, arrays of fixed-length strings occupy a
....Read More |
Rating
|
|
|
Non obvious uses for the LIKE operator
|
Total Hit (3094) |
LIKE is probably the VB operator is most underutilized if compared to its potential. The most immediate use for this operator is to check that a string begins or ends with a given pattern. For example, it is simple to check that a Product ID is made up by a alphabetic character followed by three dig
....Read More |
Rating
|
|
|
Process string characters using Byte arrays
|
Total Hit (2932) |
When you need to process each character in a string, you can assign the string to a byte array and iterate on its elements (remember: each Unicode character corresponds to two bytes). This approach is usually much faster because it saves a lot of Mid$ functions and, hence, a lot of temporary strings
....Read More |
Rating
|
|
|
Replace the last occurrence of a string
|
Total Hit (3290) |
Here is a one-liner that replaces the last occurrence of a substring in a string. It is slightly less efficient than using the InstrRev plus a Replace statement, but at least you can use it in-line inside another expression. And it is also one of the few occasions to use the StrReverse function, und
....Read More |
Rating
|
|
|
Replicate a string of any length
|
Total Hit (3093) |
The String$ function can replicate only 1-char strings, so it seems that you need a loop to duplicate strings that contain 2 or more characters. However, this is a one-liner that does the trick:
«Code LangId=1»
Function ReplicateString(Source As String, Times As Long) As String
' build a s
....Read More |
Rating
|
|
|
Retrieve the currency symbol
|
Total Hit (3142) |
It is very simple to retrieve the correct currency symbol, using just plain VBA statements:
«Code LangId=1»
currSymbol = Trim$(Replace(FormatCurrency(0,0), "0", ""))
«/Code»
If you can also determine if the symbol precedes or follows the currency amount using this method: «Code LangId=1»
If
....Read More |
Rating
|
|
|
Swap strings the fast way
|
Total Hit (3341) |
Consider the usual way of swapping two strings:
«Code LangId=1»
Dim s1 As String, s2 As String
Dim tmp As String
' initialize the strings
s1 = String$(1000, "-")
s2 = String$(1500, "+")
' do the swap, through a temporary variable
tmp = s1
s1 = s2
s2 = tmp
«/Code»
If you put the abo
....Read More |
Rating
|
|
|
Tricks with LCase and UCase
|
Total Hit (2882) |
There are a few tricks that you can do with LCase$ and UCase$ functions. I doubt you will be using this tip in all your applications, but here they are for when you'll need them.
Say you wish to check that a string (or a portion of it) does NOT include any alphabetical characters: instead of set
....Read More |
Rating
|
|
|
Two handy functions for Null handling
|
Total Hit (3182) |
You're probably aware that most VB functions don't work well with Null values, which is an issue when you're working with database columns that can accept Nulls. For example, the following statement:
«Code LangId=1»
Dim s as String
s = rs.Fields("AnyField")
«/Code»
can raise error 94 "Invali
....Read More |
Rating
|
|
|
IsValidEmail - Validate an email address
|
Total Hit (2919) |
«Code LangId=1»' Validates an email address
' Returns True if valid, False if invalid
'
' Example:
' If IsValidEmail(Value:="x@y.com", MaxLength:=255, IsRequired:=True) then ...
Function IsValidEmail(ByRef Value As String, Optional ByVal MaxLength As Long = _
255, Optional ByVal IsRequi
....Read More |
Rating
|
|
|
IsValidSSN - Check a Social Security Number value
|
Total Hit (3184) |
«Code LangId=1»' Validates attributes of the SSN
' Returns True if valid, False if invalid
'
'Example:
' If IsValidSSN(Value:="333-44-3333", IsRequired:=True) then ...
Function IsValidSSN(ByRef Value As String, Optional ByVal IsRequired As Boolean _
= True) As Boolean
On Error
....Read More |
Rating
|
|
|
CheckUSState - Validate a US state initial
|
Total Hit (2725) |
«Code LangId=1»Public Function CheckUSState(ByVal State As String) As Boolean
If Len(State) = 2 And InStr(",AL,AK,AZ,AR,CA,CO,CT,DE,DC,FL,GA,HI,ID,IL,IN,I" _
& "A,KS,KY,LA,ME,MD,MA,MI,MN,MS,MO,MT,NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,OR,P" _
& "A,RI,SC,SD,TN,TX,UT,VT,VA,WA,WV,WI,WY,",
....Read More |
Rating
|
|
|
ConvertCamelCase - Convert from a string in camel case
|
Total Hit (3462) |
«Code LangId=1»' change a sentence in CamelCase to a sentence with spaces
' for example ConvertCamelCase("FileExchange") => "File Exchange"
Public Function ConvertCamelCase(ByVal Value As String) As String
Dim i As Long
For i = 1 To Len(Trim$(Value))
' If the character is up
....Read More |
Rating
|
|
|
|
EncryptString - Encode and decode a string
|
Total Hit (3255) |
«Code LangId=1»
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _
Any, source As Any, ByVal bytes As Long)
' encrypt a string using a password
'
' you must reapply the same function (and same password) on
' the encrypted string to obtain the original, non-enc
....Read More |
Rating
|
|
|
ExplodeString - Add a filling char between a string's chars
|
Total Hit (2932) |
«Code LangId=1»' "Explode" a string by inserting a given filling character
' between consecutive characters in the original string
'
' The source string cannot contain Chr$(0) characters
Function ExplodeString(Source As String, Optional fillChar As String = " ") As _
String
ExplodeSt
....Read More |
Rating
|
|
|
FilterString - Remove invalid characters from a string
|
Total Hit (3235) |
«Code LangId=1»' Filter out all invalid characters in a string.
Function FilterString(text As String, ValidChars As String) As String
Dim i As Long, result As String
For i = 1 To Len(text)
If InStr(ValidChars, Mid$(text, i, 1)) Then
result = result & Mid$(text, i,
....Read More |
Rating
|
|
|
FormatCreditCard - Format a credit card number
|
Total Hit (4140) |
«Code LangId=1»' Format a credit card number
Function FormatCreditCard(ByVal text As String) As String
Dim i As Long
' ignore empty strings
If Len(text) = 0 Then Exit Function
' get rid of dashes, spaces and invalid chars
For i = Len(text) To 1 Step -1
....Read More |
Rating
|
|
|
|
FormatPhoneNumber - Format a phone number
|
Total Hit (3155) |
«Code LangId=1»' Modify a phone-number to the format "XXX-XXXX" or "(XXX) XXX-XXXX".
Function FormatPhoneNumber(ByVal text As String) As String
Dim i As Long
' ignore empty strings
If Len(text) = 0 Then Exit Function
' get rid of dashes and invalid chars
For i
....Read More |
Rating
|
|
|
|
GetStringBetweenTags - Returns a string between 2 delimiters
|
Total Hit (3105) |
«Code LangId=1»' Returns a string between 2 delimiters
' Parameters:
' sSearchIn: String to search
' sFrom: First keyword
' sUntil: Second keywords
' nPosAfter: Gets the position after
'
' Example:
' Debug.Print GetStringBetweenTags("<html>This is a sample of title</html>",
'
....Read More |
Rating
|
|
|
|
InstrLast - Find the last occurrence of a substring
|
Total Hit (2915) |
«Code LangId=1»
' returns the last occurrence of a substring
' The syntax is similar to InStr
Function InstrLast(ByVal Start As Long, Source As String, search As String, _
Optional CompareMethod As VbCompareMethod = vbBinaryCompare) As Long
Do
' search the next occurrence
....Read More |
Rating
|
|