Tools - String Utility Class
cToolsStr - String Processing Tools
Overview
Provides rich string processing functionality, including regex extraction, line splitting, encoding detection, string slicing, random string generation, etc.
Text Splitting and Joining
ParseNumbers
Extracts numbers from string (supports decimals).
Function ParseNumbers(inputString As String, Optional DecimalPlaces As Long = -1, Optional Count As Long) As CollectionParameters:
| Parameter | Type | Description |
|---|---|---|
inputString | String | Input string |
DecimalPlaces | Long | Decimal places, -1 means no processing |
Count | Long | Extraction count limit, 0 means unlimited |
Example:
Dim Numbers As Collection
Set Numbers = VBMAN.ToolsStr.ParseNumbers("Price: 123.5元, Quantity: 10个")
Dim n As Variant
For Each n In Numbers
Debug.Print n ' Output: 123.5, 10
Next nSplitLinesToCollection
Splits text by lines into collection.
Public Function SplitLinesToCollection(ByVal Text As String) As cCollectionExample:
Dim Lines As cCollection
Set Lines = VBMAN.ToolsStr.SplitLinesToCollection("Line1" & vbCrLf & "Line2")
Debug.Print Lines(1) ' Output: Line1JoinLinesFromCollection
Joins line collection into string.
Public Function JoinLinesFromCollection(Coll As cCollection) As StringExample:
Dim Lines As cCollection
Set Lines = New cCollection
Lines.Add "Line1"
Lines.Add "Line2"
Dim Text As String
Text = VBMAN.ToolsStr.JoinLinesFromCollection(Lines)
Debug.Print Text ' Output: Line1\r\nLine2SplitLines
Splits text by lines into array.
Public Function SplitLines(ByVal Text As String) As String()Example:
Dim Lines() As String
Lines = VBMAN.ToolsStr.SplitLines("Line1" & vbCrLf & "Line2")
Debug.Print Lines(0) ' Output: Line1String Query and Check
HasStr
Checks if string contains substring (returns position, 0 means not found).
Public Function HasStr(ByVal FindStr As String, FullStr As String, Optional StartPos As Long = 1, Optional CompType As VbCompareMethod = vbTextCompare) As LongExample:
If VBMAN.ToolsStr.HasStr("test", "this is a test") > 0 Then
Debug.Print "Contains 'test'"
End If
' Specify start position
Dim Pos As Long
Pos = VBMAN.ToolsStr.HasStr("a", "banana", 2)
Debug.Print Pos ' Output: 4HasStrFromRight
Checks if string contains substring from right side.
Public Function HasStrFromRight(ByVal FindStr As String, FullStr As String, Optional StartPos As Long = -1, Optional CompType As VbCompareMethod = vbTextCompare) As LongExample:
Dim Pos As Long
Pos = VBMAN.ToolsStr.HasStrFromRight("a", "banana")
Debug.Print Pos ' Output: 6 (position of last 'a')IsEmptyEx
Checks if string is empty (after trimming whitespace).
Public Function IsEmptyEx(Text As String) As BooleanExample:
Debug.Print VBMAN.ToolsStr.IsEmptyEx(" ") ' Output: True
Debug.Print VBMAN.ToolsStr.IsEmptyEx("hello") ' Output: FalseIsString
Checks if variable is string type.
Public Function IsString(var As Variant) As BooleanExample:
Debug.Print VBMAN.ToolsStr.IsString("hello") ' Output: True
Debug.Print VBMAN.ToolsStr.IsString(123) ' Output: FalseString Slicing and Substring
SliceString
Processes String type slicing.
Public Function SliceString(ByVal Arr As String, ByVal StartPos As Long, Optional ByVal EndPos As Long = -1) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Arr | String | Source string |
StartPos | Long | Start position (0-based) |
EndPos | Long | End position (0-based), -1 means to end |
Example:
Dim str As String
Dim sliced As String
str = "Hello World"
sliced = VBMAN.ToolsStr.SliceString(str, 0, 4)
Debug.Print sliced ' Output: Hello
' From position 6 to end
sliced = VBMAN.ToolsStr.SliceString(str, 6)
Debug.Print sliced ' Output: WorldSubStr
Extracts substring by start and end markers.
Public Function SubStr(ByVal Txt As String, ByVal txtFirst As String, Optional ByVal txtEnd As String, Optional RetInt As Boolean, Optional Method As VbCompareMethod = vbBinaryCompare, Optional FindFromEnd As Boolean) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Txt | String | Source string |
txtFirst | String | Start marker |
txtEnd | String | End marker, empty means to end |
RetInt | Boolean | Return "0" when not found instead of empty string |
Method | VbCompareMethod | Comparison method |
FindFromEnd | Boolean | Find end marker from end |
Example:
Dim Text As String
Dim Result As String
Text = "<div>Hello World</div>"
' Extract content inside div tag
Result = VBMAN.ToolsStr.SubStr(Text, "<div>", "</div>")
Debug.Print Result ' Output: Hello World
' Find from end
Result = VBMAN.ToolsStr.SubStr(Text, "<", ">", False, vbBinaryCompare, True)
Debug.Print Result ' Output: /divMidEx
Extracts substring by start and end markers (with position tracking).
Public Function MidEx(FullStr As String, Lstr As String, Rstr As String, Optional starindex As Long) As StringExample:
Dim Text As String
Text = "name[John]age[25]city[Beijing]"
Dim Result As String
Dim StartPos As Long
Result = VBMAN.ToolsStr.MidEx(Text, "[", "]", StartPos)
Debug.Print Result ' Output: John
Debug.Print StartPos ' Output: Next search start positionToArray
Converts string to character array.
Public Function ToArray(Text As String) As String()Example:
Dim CharArray() As String
CharArray = VBMAN.ToolsStr.ToArray("Hello")
Dim i As Long
For i = LBound(CharArray) To UBound(CharArray)
Debug.Print CharArray(i) ' Output: H e l l o
Next iString Trimming and Processing
TrimEx
Trims whitespace characters from beginning and end of string (including invisible characters).
Public Function TrimEx(ByRef Text As String, Optional IsLeft As Boolean = True, Optional IsRight As Boolean = True) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Text | String | Source string |
IsLeft | Boolean | Whether to trim left whitespace |
IsRight | Boolean | Whether to trim right whitespace |
Example:
Dim Text As String
Text = vbCrLf & " Hello World " & vbTab
Debug.Print VBMAN.ToolsStr.TrimEx(Text) ' Output: Hello World
Debug.Print VBMAN.ToolsStr.TrimEx(Text, True, False) ' Output: Hello World \t
Debug.Print VBMAN.ToolsStr.TrimEx(Text, False, True) ' Output: \r\n Hello WorldRightEx / LeftEx
Gets specified length of characters from right/left of string.
Public Function RightEx(Text As Variant, Length As Long) As String
Public Function LeftEx(Text As Variant, Length As Long) As StringExample:
Dim Text As String
Text = " Hello World "
Debug.Print VBMAN.ToolsStr.RightEx(Text, 5) ' Output: World
Debug.Print VBMAN.ToolsStr.LeftEx(Text, 5) ' Output: HelloInsertSpan
Inserts separator every specified number of characters.
Public Function InsertSpan(ByRef inputStr As String, ByVal Span As String, ByVal SetpNum As Long, Optional HeadFoot As Boolean) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
inputStr | String | Source string |
Span | String | Separator |
SetpNum | Long | Insert separator every how many characters |
HeadFoot | Boolean | Whether to add separator at beginning and end |
Example:
Dim Text As String
Text = "1234567890123456"
' Insert space every 4 characters
Debug.Print VBMAN.ToolsStr.InsertSpan(Text, " ", 4) ' Output: 1234 5678 9012 3456
Debug.Print VBMAN.ToolsStr.InsertSpan(Text, "-", 4, True) ' Output: -1234-5678-9012-3456-String Encoding Conversion
PercentEncode / PercentDecode
Percent encoding/decoding (converts numbers to % format).
Public Function PercentEncode(inputStr As String) As String
Function PercentDecode(encodedStr As String) As StringExample:
Dim Text As String
Text = "43068119891129321X"
Dim Encoded As String
Encoded = VBMAN.ToolsStr.PercentEncode(Text)
Debug.Print Encoded ' Output: %34%33%30%36%38%31%31%39%38%39%31%31%32%39%33%32%31X
Dim Decoded As String
Decoded = VBMAN.ToolsStr.PercentDecode(Encoded)
Debug.Print Decoded ' Output: 43068119891129321XUnicodeEncode / UnicodeDecode
Unicode encoding/decoding (\uXXXX format).
Public Function UnicodeEncode(ByVal inputString As String, Optional PreFix As String = "\u") As String
Public Function UnicodeDecode(Text As String, Optional PreFix As String = "\u") As StringExample:
Dim Text As String
Text = "你好 World"
Dim Encoded As String
Encoded = VBMAN.ToolsStr.UnicodeEncode(Text)
Debug.Print Encoded ' Output: \u4F60\u597D World
Dim Decoded As String
Decoded = VBMAN.ToolsStr.UnicodeDecode(Encoded)
Debug.Print Decoded ' Output: 你好 WorldByte Array Operations
ToBytes
Converts string to byte array (Unicode).
Public Function ToBytes(inputString As String) As Byte()Example:
Dim Bytes() As Byte
Bytes = VBMAN.ToolsStr.ToBytes("Hello")FromByteArray
Converts byte array to string.
Public Function FromByteArray(inputArray() As Byte, Optional CharSet As String = "UTF-8") As StringExample:
Dim Bytes() As Byte
Bytes = VBMAN.ToolsStr.ToBytes("Hello")
Dim Text As String
Text = VBMAN.ToolsStr.FromByteArray(Bytes)
Debug.Print Text ' Output: HelloLenBytes
Calculates byte array length.
Public Function LenBytes(inputArray() As Byte) As LongExample:
Dim Bytes() As Byte
Bytes = VBMAN.ToolsStr.ToBytes("Hello")
Debug.Print VBMAN.ToolsStr.LenBytes(Bytes) ' Output: 5ToHex / FromHex
String and hexadecimal conversion.
Public Function ToHex(InputData As Variant, Optional CharSet As String = "UTF-8") As String
Public Function FromHex(hexStr As String, Optional CharSet As String = "UTF-8") As StringExample:
Dim Text As String
Text = "Hello"
' Convert to hexadecimal
Dim HexStr As String
HexStr = VBMAN.ToolsStr.ToHex(Text)
Debug.Print HexStr ' Output: 48656C6C6F
' Restore from hexadecimal
Dim Original As String
Original = VBMAN.ToolsStr.FromHex(HexStr)
Debug.Print Original ' Output: HelloRandom String Generation
GetRandStr
Gets random string.
Public Function GetRandStr(Optional ByVal Lens As Long = 32, Optional Zuhe As String = "1aA") As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Lens | Long | String length |
Zuhe | String | Character combination: 1=numbers, a=lowercase, A=uppercase, @=special characters |
Example:
' Numbers only, 8 characters
Debug.Print VBMAN.ToolsStr.GetRandStr(8, "1")
' Numbers + lowercase letters, 16 characters
Debug.Print VBMAN.ToolsStr.GetRandStr(16, "1a")
' Numbers + uppercase/lowercase letters, 32 characters
Debug.Print VBMAN.ToolsStr.GetRandStr(32, "1aA")
' Include special characters
Debug.Print VBMAN.ToolsStr.GetRandStr(16, "1aA@")GetRandByte / GetRandByteToHex
Gets random byte array.
Public Function GetRandByte(Optional ByteSize As Long = 32) As Byte()
Public Function GetRandByteToHex(Optional ByteSize As Long = 32, Optional Span As String = "") As StringExample:
' Get 32-byte random array
Dim Bytes() As Byte
Bytes = VBMAN.ToolsStr.GetRandByte(32)
' Get 16-byte random string (hexadecimal)
Dim HexStr As String
HexStr = VBMAN.ToolsStr.GetRandByteToHex(16)
Debug.Print HexStr ' Output: a3f7b2c8d1e5...
' With separator
HexStr = VBMAN.ToolsStr.GetRandByteToHex(16, "-")
Debug.Print HexStr ' Output: a3-f7-b2-c8-...GUID Generation
GetGUID
Generates GUID string.
Public Function GetGUID(Optional isFull As Boolean) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
isFull | Boolean | Whether to return full format (with brackets) |
Example:
' Standard GUID format
Debug.Print VBMAN.ToolsStr.GetGUID() ' Output: 550e8400-e29b-41d4-a716-446655440000
' Full format (with brackets)
Debug.Print VBMAN.ToolsStr.GetGUID(True) ' Output: {550e8400-e29b-41d4-a716-446655440000}Helper Functions
JoinStr
Joins multiple strings with specified separator.
Public Function JoinStr(Span As String, ParamArray Strings() As Variant) As StringExample:
Dim Result As String
Result = VBMAN.ToolsStr.JoinStr("-", "2024", "05", "17")
Debug.Print Result ' Output: 2024-05-17UniVbCrLf
Unifies line breaks to vbCrLf.
Public Function UniVbCrLf(Text As String) As StringExample:
Dim Text As String
Text = "Line1" & vbLf & "Line2" & vbCr & "Line3"
Dim Result As String
Result = VBMAN.ToolsStr.UniVbCrLf(Text)
' Result: Line1\r\nLine2\r\nLine3GetFirstChar / GetLastChar
Gets first/last valid character.
Public Function GetFirstChar(Txt As String, Optional Length As Long = 1) As String
Public Function GetLastChar(Txt As String, Optional Length As Long = 1) As StringExample:
Dim Text As String
Text = " Hello World "
Debug.Print VBMAN.ToolsStr.GetFirstChar(Text) ' Output: H
Debug.Print VBMAN.ToolsStr.GetLastChar(Text) ' Output: d
Debug.Print VBMAN.ToolsStr.GetFirstChar(Text, 5) ' Output: HellocFormater - General Formatter
Overview
General-purpose formatter with chainable interface, can format file sizes, time, numbers, etc.
Chain Entry
Data
Sets the data source to format.
Public Function Data(ByVal Source As Variant) As cFormaterFile Size Formatting
ReturnFileSize
Formats file size to human-readable format (B/KB/MB/GB/TB).
Public Function ReturnFileSize(Optional ByVal DecimalPlaces As Long = 2, Optional ByVal ForceUnit As String = "") As StringExample:
' Auto-select unit
Debug.Print VBMAN.Formater.Data(1536).ReturnFileSize() ' 1.50 KB
Debug.Print VBMAN.Formater.Data(1572864).ReturnFileSize() ' 1.50 MB
' Force unit
Debug.Print VBMAN.Formater.Data(1024).ReturnFileSize(2, "KB") ' 1.00 KB
Debug.Print VBMAN.Formater.Data(1024).ReturnFileSize(2, "B") ' 1024.00 BTime Formatting
ReturnRelativeTime
Returns relative time description (seconds ago, minutes ago, etc.).
Public Function ReturnRelativeTime() As StringExample:
Dim Ts As Currency
Ts = VBMAN.ToolsDateTime.GetUnixTimestamp() - 60000 ' 1 minute ago
Debug.Print VBMAN.Formater.Data(Ts).ReturnRelativeTime() ' 1 minute agoReturnDuration
Formats duration.
Public Function ReturnDuration(Optional ByVal Precision As String = "auto") As StringExample:
Debug.Print VBMAN.Formater.Data(65000).ReturnDuration() ' 1m 5s
Debug.Print VBMAN.Formater.Data(3661000).ReturnDuration() ' 1h 1m 1sNumber Formatting
ReturnNumber
Formats number (thousands separator, decimal places, etc.).
Public Function ReturnNumber(Optional ByVal DecimalPlaces As Long = -1, Optional ByVal UseThousandSeparator As Boolean = True) As StringExample:
Debug.Print VBMAN.Formater.Data(1234567.89).ReturnNumber() ' 1,234,567.89
Debug.Print VBMAN.Formater.Data(1234567.89).ReturnNumber(0) ' 1,234,568Complete Example
Private Sub FormatterDemo()
' File size
Dim FileSize As Currency
FileSize = FileLen("C:\\largefile.zip")
lblSize.Caption = VBMAN.Formater.Data(FileSize).ReturnFileSize()
' Number thousands separator
Dim Amount As Currency
Amount = 1234567.89
lblAmount.Caption = VBMAN.Formater.Data(Amount).ReturnNumber(2)
' Relative time
Dim LastTime As Currency
LastTime = GetLastLoginTime() ' Unix timestamp
lblLastLogin.Caption = VBMAN.Formater.Data(LastTime).ReturnRelativeTime()
End Sub