|
Determine whether a string is lowercase
|
Total Hit (3312) |
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 (3344) |
.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 (3823) |
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 (2942) |
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 (2811) |
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
|
|
|
Process string faster with the StringBuilder class
|
Total Hit (2926) |
You can think of a StringBuilder object as a buffer that can contain a string with the ability to grow from zero characters to the buffer's current capacity. Until you exceed that capacity, the string is assembled in the buffer and no object is allocated or released. If the string becomes longer tha
....Read More |
Rating
|
|
|
Reducing string memory usage with the Intern method
|
Total Hit (3090) |
A .NET application can optimize string management by maintaining an internal pool of string values known as intern pool. If the value being assigned to a string variable coincides with one of the strings already in the intern pool, no additional memory is created and the variable receives the addres
....Read More |
Rating
|
|
|
Right-align formatted strings
|
Total Hit (3443) |
The String.Format function supports many formatting options, but none allows you to display right-aligned columns of numbers, as in:
1.00
12.00
123.00
1,234.00
However, you can easily create a helper function that works like String.Format, takes an additional length argument,
....Read More |
Rating
|
|
|
Trimming strings
|
Total Hit (3126) |
Visual Basic .NET strings expose three trim methods: TrimStart, TrimEnd, and Trim - which trim one or more characters from the beginning, the end, or both the beginning and end of the string. They therefore work like VB6's LTrim, RTrim, and Trim functions, with an important difference: you can decid
....Read More |
Rating
|
|
|
IsValidEmail - Validate an email address
|
Total Hit (2700) |
«Code LangId=2»
Function IsValidEmail(ByVal Value As String, Optional ByVal MaxLength As _
Integer = 255, Optional ByVal IsRequired As Boolean = True) As Boolean
If Value Is Nothing OrElse Value.Length = 0 Then
' rule out the null string case
Return Not IsRequired
....Read More |
Rating
|
|
|
IsValidUsPhoneNumber - Validating a US phone number
|
Total Hit (3082) |
«Code LangId=2»
' Validate a US phone number
' Example:
' MessageBox.Show(IsValidUsPhoneNumber("(123) 456-7890")) ' True
' MessageBox.Show(IsValidUsPhoneNumber("(123) 456-78901")) ' False
Function IsValidUsPhoneNumber(ByVal phnNum As String) As Boolean
Return System.Text.RegularExp
....Read More |
Rating
|
|
|
|
ConcatenateStrings - Concatenating an array of strings
|
Total Hit (2796) |
«Code LangId=2»' Concatenate the input strings, and return the resulting string. The first
' input string is used as a separator.
'
' Example:
' Dim i As Integer = 4
' Dim d As Double = 34.45
' Dim s As String = "VB-2-The-Max"
' Dim ret As String = ConcatenateStrings(" ", s, "is a n
....Read More |
Rating
|
|
|
CountOccurrences - Counting the number of string occurrences
|
Total Hit (2923) |
«Code LangId=2»
' Count the number of string occurrences
Public Function CountOccurrences(ByVal source As String, ByVal search As String, _
Optional ByVal ignoreCase As Boolean = False) As Integer
Dim options As System.Text.RegularExpressions.RegexOptions
' set the search options a
....Read More |
Rating
|
|
|
DecodeBase64 - Decoding a string from base64
|
Total Hit (2960) |
«Code LangId=2»' Returns the input string decoded from base64
Private Function DecodeBase64(ByVal input As String) As String
Dim strBytes() As Byte = System.Convert.FromBase64String(input)
Return System.Text.Encoding.UTF8.GetString(strBytes)
End Function
«/Code»
....Read More |
Rating
|
|
|
EncodeBase64 - Encoding a string to base64
|
Total Hit (3188) |
«Code LangId=2»' Returns the input string encoded to base64
Private Function EncodeBase64(ByVal input As String) As String
Dim strBytes() As Byte = System.Text.Encoding.UTF8.GetBytes(input)
Return System.Convert.ToBase64String(strBytes)
End Function
«/Code»
....Read More |
Rating
|
|
|
|
ExplodeString - Add a filling char between a string's chars
|
Total Hit (2589) |
«Code LangId=2»
' "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(ByVal Source As String, Optional ByVal fillChar As Char = _
" "c) As String
....Read More |
Rating
|
|
|
|
FormatMemorySize - Format a value in bytes
|
Total Hit (2841) |
«Code LangId=2»
Enum FormatMemorySizeUnits
BestGuess
Bytes
Kilobytes
Megabytes
Gigabytes
End Enum
' convert a number of bytes into Kbytes, Megabytes, or Gigabytes
Function FormatMemorySize(ByVal value As Long, _
ByVal unit As FormatMemorySizeUnits, Optional B
....Read More |
Rating
|
|
|
FormatSize - Format a size expressed in bytes
|
Total Hit (2549) |
«Code LangId=2»
' Returns a formatted string that shows the size in Bytes, KBytes or MBytes
' according to the size
' Usage:
' Dim ProperSizeString As String = FormatSize("132100842")
' -> returns "125.98 MB"
Function FormatSize(ByVal SizeInBytes As Double) As String
If SizeInBytes
....Read More |
Rating
|
|
|
FormatValue - Format a value in a column of given width
|
Total Hit (2825) |
«Code LangId=2»
Enum FormatColumnAlignment
Left
Center
Right
End Enum
' format a value in a column of given width and with specified alignment
' using the specified pad character
Function FormatValue(ByVal value As String, ByVal width As Integer, _
ByVal alignment As F
....Read More |
Rating
|
|
|
|
|
|
|
InstrTbl - Search a string for any character in a table
|
Total Hit (3660) |
«Code LangId=2»
' If INCLUDE is True or is omitted, return the first occurrence of a character
' in a group
' or -1 if SOURCE doesn't contain any character among those listed in TABLE.
' If INCLUDE is False, return the first occurrence of the character in SOURCE
' that does not appear in TABL
....Read More |
Rating
|
|
|
InstrTblRev - The last occurrence of a char in a table
|
Total Hit (3453) |
«Code LangId=2»' If INCLUDE is True or is omitted, return the last occurrence of a character
' in a group
' or -1 if SOURCE doesn't contain any character among those listed in TABLE.
' If INCLUDE is False, return the last occurrence of the character in SOURCE
' that does not appear in TABLE.
....Read More |
Rating
|
|
|