logo VB.Net - Strings
Guide Contents
DigitalDan Home Page

Approximate Word Count
Private Shared Function WordCount(text As String) As Integer
Dim wordColl As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(text, "[\S]+")
Return wordColl.Count
End Function

 
Left(string,integer) and Right(string,integer)
Private Shared Function Lleft(text As String, length As Integer) As String
' alternative to Microsoft.VisualBasic.Left(text,length)
If length < 1 Then Return ""
If length >= Len(text) Then Return text
Return Mid(text, 1, length)
End Function
'
Private Shared Function Rright(text As String, length As Integer) As String
' alternative to Microsoft.VisualBasic.Right(text,length)
If length < 1 Then Return ""
If length >= Len(text) Then Return text
Return Mid(text, Len(text) - length + 1, length)
End Function

 
Remove Chars from middle of string
Private Shared Function RemoveCharacters(text As String, start As Integer, numberOfCharacters As Integer) As String
Return text.Remove(start, numberOfCharacters)
End Function

 
Instert Text Into String
Private Shared Function InsertCharacters(originalText As String, insertAt As Integer, toBeInserted As String) As String
Return originalText.Insert(insertAt, toBeInserted)
End Function

 
Remove Chars from middle of string
Private Shared Function RemoveCharacters(text As String, start As Integer, numberOfCharacters As Integer) As String
Return text.Remove(start, numberOfCharacters)
End Function

 
Convert Number .ToString()
VB.Net has a versetile built in command to convert numbers into a string. It allows to to specify exactly what format to use. This feature is called .ToString()
The syntax for .ToString is as follows Dim text as String = number.ToString("___")

 
Where "___" can be ommitted or contain any combination of the following Characters
#If Digit Is part Or number Then use the digit, otherwis pad the number With a leading/trailing space
0If Digit Is part of number then use the digit, otherwise pad number with a leading/trailing zero
;Anything before ; applies to +ve numbers, all after ; applies to negative numbers
.Decimal Point to be shown at this position
,.Divide number by 1000 (,,. = /1000000 etc.)
,(Not next to .) - show a "thousand separator" in this position
CUse a currency symbol{-£1,234.57}
E+Use Exponential notation e.g. "00E+00" {-12E+02}
E-The same as E+ but omit the + sign if exponential Is positive
FNumber of digits after decimal point e.g. "F2" {-1234.57}
GShortest of F Or E+ e.g. "G2" {-1.2E+03}
NSet number of decimal places & add thousand separators e.g. "N2" {-1,234.57}
PConvert to percent with thousand separators e.g. "P2" {-123,456.79%}
RUsed to avoid rounding errors in certain circumstances {-1234.56789}
\Escape charater (Insert next charater into the string, even if it normally has a special meaning)
DForce a minimum number of digits (pad with 0) e.g 1234 "D7" {0001234} This Is minimum digits, number will Not truncate e.g. -1234 "D2" {-1234}
XConvert to Hex using 0-9 And A-F e.g. 1234 {4D2} Negative numbers give a 2's compliment e.g. -1234 {FFFFFb2e}
dIdentical to "D"
xConvert to Hex using 0-9 And a-f e.g. 1234 {4d2} Negative numbers give a 2's compliment e.g. -1234 {fffffb2e}
Here are some examples
Dim num As Double = 12.34
Dim txt as string
txt = num.ToString() ' text set to "12.34"
txt = num.ToString("000.000") ' txt set to "012.340"
txt = num.ToSTring("###000.0##") ' txt set to "012.34"
txt = num.Tostring("##0 . 0##") ' txt set to "12 . 34"

 
String.Join()
This built-in function will merge the contents of either a List or Array of string and output the result as string. For Arrays we can say
 
Private Function Merge_StringArray(separator As String, array() As String) As String
Return String.Join(separator, array)
End Function

separator = string to be inserted between each line of array (typically "", "," or vbCrLf)
array = array containing the set of strings to be combined
 
The function will also work with List(of String)
 
Private Function Merge_List_Of_String(separator As String, listString As List(Of String)) As String
Return String.Join(separator, listString)
End Function

 
string.Split()
These functions take a string and split it into an arroy (or list) of string parts. Every time it comes accross the separator string (typically "," or vbCrLf) is splits the string. The separator charactors are removed during the process and wil not be present in the output.
 
Private Function Split_String(separator As String, text As String, splitOptions As StringSplitOptions) As String()
Return text.Split(separator, splitOptions)
End Function

 
separator = character/string used to identify where string should be split
text - String to be split into parts
splitOptions = "StringSplitOptions.None" "StringSplitOptions.RemoveEmptyEntries" or "StringSplitOptions.TrimEntries"
The default when stringOtions is omitted is "StringSplitOptions.None"
 
string.Split can also split the string into List(of String)
 
Private Function Split_String(separator As String, text As String, splitOptions As StringSplitOptions) As List(Of String)
Return text.Split(separator, splitOptions).ToList
End Function

 
Split(string ...)
The main differences between string.Split and Split(string ...) are

 
Private Function Split_String(separator As String, text As String, maxParts As Integer, comparetype As CompareMethod) As String()
Return Split(text, separator, maxParts, comparetype)
End Function

 
separator = character/string used to identify where string should be split
text = String to be split into parts
maxParts = Integer, maximum number of parts before funtions stops splitting string
comparetype = "CompareMethod.Binary" or "CompareMethod.Text"
maxParts can be ommitted, the default value is -1. Setting maxParts to -1 will allow teh string to split every time separator is found.
compareType can be omitted, the default value of "CompareMethod.Text" is suitable for most "human readable" text strings.
 
Split(string ...) can also split the string into List(of String)
 
Private Function Split_String(separator As String, text As String, maxParts As Integer, comparetype As CompareMethod) As List(Of String)
Return Split(text, separator, maxParts, comparetype).ToList
End Function

DigitalDan.co.uk ... Hits = 119