|
Determine whether a string is lowercase
|
Total Hit (3313) |
Here's a quick-and-dirty method to determine whether a string contains only lowercase characters
«Code LangId=2»
' the string to test is in the SOURCE variable
If System.Text.RegularExpressions.Regex.IsMatch(source, "^[a-z]+$") Then
Console.WriteLine("Lowercase string")
End If
«/Code»
....Read More |
Rating
|
|
|
Determine whether a string is uppercase
|
Total Hit (3211) |
Here's a quick-and-dirty method to determine whether a string contains only uppercase characters
«Code LangId=2»
' the string to test is in the SOURCE variable
If System.Text.RegularExpressions.Regex.IsMatch(source, "^[A-Z]+$") Then
Console.WriteLine("Uppercase string")
End If
«/Code»
....Read More |
Rating
|
|
|
Faster string comparison with the Is operator
|
Total Hit (3345) |
.NET strings are objects, so what you store in a String variable is actually a reference to a String object allocated in the managed heap. Even though the VB.NET compiler attempts to hide the object nature of strings as much as it can - for example, you don't need to declare a String object with a N
....Read More |
Rating
|
|
|
Faster string comparisons with CompareOrdinal
|
Total Hit (3824) |
You can compare strings in many ways under VB.NET: by using the = operator (or another comparison operator), with the Equals method (which the String class inherits from System.Object), or with the Compare shared method. The Compare method is especially useful because it returns an integer that tell
....Read More |
Rating
|
|
|
Iterating over the characters in a string
|
Total Hit (2944) |
Visual Basic .NET strings support the For Each statement, so you can iterate over each individual character as follows:
«Code LangId=2»
Dim s As String = "ABCDE"
Dim c As Char
For Each c In s
Console.Write(c & ".") ' => A.B.C.D.E.
Next
«/Code»
However, you should bear in mind th
....Read More |
Rating
|
|
|
Locale-aware string conversions
|
Total Hit (2812) |
You can use the usual UCase, LCase, and StrConv functions to convert a string to upper, lower, and titlecase (e.g. "This Is A Title"). However, VB.NET offers is much more flexible, and lets you convert the case of a string according the locale you specify. You can do this through the methods of the
....Read More |
Rating
|
|
|
Understanding the "Allow Unrounded Floating Point Operations" option
|
Total Hit (2967) |
The Microsoft manuals preach that all the compiler options in the Advanced Optimization dialog box are to be considered unsafe, in that they might lead to incorrect results (or just program crashes!). This is true for most of them, but often one of such options - namely, the "Allow Unrounded Floatin
....Read More |
Rating
|
|
|
Undocumented behavior of the CInt() function
|
Total Hit (3100) |
The CInt() function rounds to the nearest integer value. In other words, CInt(2.4) returns 2, and CInt(2.6) returns 3.
This function exhibits an under-documented behavior when the fractional part is equal to 0.5. In this case, this function rounds down if the integer portion of the argument is e
....Read More |
Rating
|
|
|
Use integer division operator
|
Total Hit (2988) |
Use "\" instead of "/" when performing divisions between Integers. The "/" operator returns a Single value, therefore the seemingly efficient line
«Code LangId=1»
C% = A% / B%
«/Code»
actually requires three implicit conversions, two for converting the operands from Integer to Single (to pre
....Read More |
Rating
|
|
|
Convert a binary, octal, or hexadecimal value to decimal
|
Total Hit (4074) |
The Convert class offers a few static methods that let you easily and quickly convert a binary, octal, or hexadecimal number(stored in a String) into the equivalent Byte, Short, Integer, or Long value. These are the methods in question:
«Code LangId=2»Dim b As Byte = Convert.ToByte(value, fromB
....Read More |
Rating
|
|
|
Convert a decimal value to binary, octal, or hexadecimal
|
Total Hit (6111) |
The ToString method of the Convert class lets you easily and quickly convert a decimal value into a string representation of that number to binary, octal, or hexadecimal base:
«Code LangId=2»' convert to binary
Console.WriteLine(Convert.ToString(11, 2)) ' => 1011
' convert to octal
Console.W
....Read More |
Rating
|
|
|
MK? And CV? - Convert numbers to strings and back
|
Total Hit (9898) |
The following routines convert a numeric value into a string that represents the number, and vice versa. They are useful for reading data written by QuickBasic programs, because the QuickBasic language functions that did the conversions were not ported to Visual Basic.
Points to note:
1. These
....Read More |
Rating
|
|
|
Take advantage of the new math functions
|
Total Hit (3084) |
The System.Math class exposes several static methods that let you perform many common operations. These functions replace the VB6 functions with same name, but there are a few new functions that have no direct VB6 counterpart:
Ceiling(x) returns the integer equal or higher than the argument
Floo
....Read More |
Rating
|
|
|
Always use "$-typed" string functions
|
Total Hit (2863) |
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 (2939) |
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 (3874) |
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
|
|
|
Extract all quoted strings with the RegExp object
|
Total Hit (3699) |
When parsing a text file that contains quoted strings - such as a VB source file - you might want to quickly locate and extract all the quoted strings. Thanks to regular expressions and the RegExp object in the Microsoft VBScript Regular Expression type library, this task is surprisingly easy:
«co
....Read More |
Rating
|
|
|
Extract words with the RegExp object
|
Total Hit (3818) |
The following routine extracts all the words from a source string and returns a collection. Optionally, the result contains only unique words.
This code is remarkably simpler than an equivalent "pure" VB solution because it takes advantage of the RegExp object in the Microsoft VBScript Regular Expr
....Read More |
Rating
|
|
|
Faster string appending with Mid$ command
|
Total Hit (3240) |
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 (2788) |
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 (2949) |
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 (3082) |
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 (2919) |
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 (3278) |
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 (3082) |
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 (3128) |
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
|
|
|
Search multiple substrings with the RegExp object
|
Total Hit (3856) |
The RegExp object in the Microsoft VBScript Regular Expression type library supports regular expression patterns containing the | (or) operator, which lets you search for multiple substrings at the same time. For example, the following piece of code lets you search for a month name in a source text:
....Read More |
Rating
|
|
|
Swap strings the fast way
|
Total Hit (3332) |
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 (2870) |
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 (3175) |
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
|
|