📄 cstring.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "cString"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2004 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: cString
'
''
' Provides static functions to format and manipulate string values.
'
' @remarks This class cannot be directly instantiated. The methods are accessed
' through the class name itself.
' <pre>
' s = cString.Trim(s, "\")
' </pre>
'
Option Explicit
Private mBuilder As StringBuilder
Private mString As WordBuffer
Private mTrimChars As WordBuffer
Private mJoinSA As SafeArray1d
Private mJoinArray() As String
''
' Returns if the string is null.
'
' @param s The string to be tested for null.
' @return Indication if the string is null.
' @remarks If the string is null it will return True. An empty string
' will return false. A null string is equal to StrPtr(s) = 0.
'
Public Function IsNull(ByRef s As String) As Boolean
IsNull = (StrPtr(s) = vbNullPtr)
End Function
''
' Returns a string from the characters in an integer array.
'
' @param Chars The array to create the string from.
' @param Index The starting index in the array.
' @param Count The number of characters to use.
' @return A string created from the chars in an integer array.
'
Public Function FromCharArray(ByRef Chars() As Integer, Optional ByRef Index As Variant, Optional ByRef Count As Variant) As String
Dim ElemIndex As Long
Dim ElemCount As Long
Dim Result As Long
Result = GetOptionalArrayRange(SAPtr(Chars), Index, ElemIndex, Count, ElemCount)
If Result <> NO_ERROR Then Call ThrowArrayRangeException(Result, "Chars", ElemIndex, "Index", ElemCount, "Count", IsMissing(Index))
If ElemCount = 0 Then
' We don't want a Null string returned.
FromCharArray = ""
Exit Function
End If
FromCharArray = SysAllocStringLen(VarPtr(Chars(ElemIndex)), ElemCount)
End Function
''
' Formats a string by replacing each argument with the formatted equivalent.
'
' @param fmt The string containing formatting information.
' @param args A list of arguments to use in replacing the formatting information.
' @return A formatted string.
' @see NumberFormatInfo
' @see DateTimeFormatInfo
' @include "..\Includes\cString.Format.txt"
Public Function Format(ByRef fmt As String, ParamArray args() As Variant) As String
Dim vArgs() As Variant
Call Helper.Swap4(ByVal ArrPtr(vArgs), ByVal Helper.DerefEBP(16))
Format = FormatArrayEx(Nothing, fmt, vArgs)
End Function
''
' Formats a string by replacing each argument with the formatted equivalent.
'
' @param provider A custom format provider that is used to format the argument instead of the default formatting.
' @param fmt The string containing formatting information.
' @param args A list of arguments to use in replacing the formatting information.
' @return A formatted string.
' @see NumberFormatInfo
' @see DateTimeFormatInfo
'
Public Function FormatEx(ByVal Provider As IFormatProvider, ByRef fmt As String, ParamArray args() As Variant) As String
Dim vArgs() As Variant
Call Helper.Swap4(ByVal ArrPtr(vArgs), ByVal Helper.DerefEBP(20))
FormatEx = FormatArrayEx(Provider, fmt, vArgs)
End Function
''
' Formats a string by replacing each argument with the formatted equivalent.
'
' @param fmt The string containing formatting information.
' @param args A list of arguments to use in replacing the formatting information.
' @return A formatted string.
' @see NumberFormatInfo
' @see DateTimeFormatInfo
'
Public Function FormatArray(ByRef fmt As String, ByRef args() As Variant) As String
FormatArray = FormatArrayEx(Nothing, fmt, args)
End Function
''
' Formats a string by replacing each argument with the formatted equivalent.
'
' @param provider A custom format provider that is used to format the argument instead of the default formatting.
' @param fmt The string containing formatting information.
' @param args A list of arguments to use in replacing the formatting information.
' @return A formatted string.
' @see NumberFormatInfo
' @see DateTimeFormatInfo
'
Public Function FormatArrayEx(ByVal Provider As IFormatProvider, ByRef fmt As String, ByRef args() As Variant) As String
mBuilder.Length = 0
Call mBuilder.InternalAppendFormat(Provider, fmt, args)
FormatArrayEx = mBuilder.ToString
End Function
''
' Creates and array of chars (Integers) from the specified string.
'
' @param s The string to create the chars from.
' @param startindex The start index in the string to begin converting to a char array. This is zero-based.
' @param length The number of characters to convert to chars.
' @return An array containing the converted characters from the string.
'
Public Function ToCharArray(ByRef s As String, Optional ByRef StartIndex As Variant, Optional ByRef Length As Variant) As Integer()
Dim ElemIndex As Long
Dim ElemCount As Long
Dim Result As Long
Result = GetOptionalListRange(Len(s), StartIndex, ElemIndex, Length, ElemCount)
If Result <> NO_ERROR Then Call ThrowListRangeException(Result, ElemIndex, "StartIndex", ElemCount, "Length", IsMissing(StartIndex))
Dim Ret() As Integer
If ElemCount > 0 Then
ReDim Ret(0 To ElemCount - 1)
Call CopyMemory(Ret(0), ByVal StrPtr(s) + ElemIndex * 2, ElemCount * 2)
Else
Ret = Cor.NewIntegers()
End If
ToCharArray = Ret
End Function
''
' Pads a string's left side.
'
' @param s The string to be padded.
' @param totalWidth The total length of the final string.
' @param paddingChar The character to pad the left of the string with.
' @return The padded string.
' @remarks The paddingChar parameter can accept either a number or chr$ value.
'
Public Function PadLeft(ByRef s As String, ByVal totalWidth As Long, Optional ByRef paddingChar As Variant = 32) As String
Dim l As Long
l = Len(s)
If totalWidth > l Then
PadLeft = String$(totalWidth - l, paddingChar) & s
Else
PadLeft = s
End If
End Function
''
' Pads a string's right side.
'
' @param s The string to be padded.
' @param totalWidth The total length of the final string.
' @param paddingChar The character to pad the right of the string with.
' @return The padded string.
' @remarks The paddingChar parameter can accept either a number or chr$ value.
'
Public Function PadRight(ByRef s As String, ByVal totalWidth As Long, Optional ByRef paddingChar As Variant = 32) As String
Dim l As Long
l = Len(s)
If totalWidth > l Then
PadRight = s & String$(totalWidth - l, paddingChar)
Else
PadRight = s
End If
End Function
''
' Trims the beginning of the string.
'
' @param s The string to be trimmed.
' @param trimChars The characters to remove.
' @return The trimmed string.
' @remarks Unlike VB's LTrim function, this can trim multiple character types,
' not just spaces.
' <p>The <i>trimChars</i> can be either a string of the characters or an integer array
' of characters that will be trimmed from the beginning.</p>
'
Public Function TrimStart(ByRef s As String, Optional ByRef TrimChars As Variant) As String
Dim Chars() As Integer
Dim StartIndex As Long
Select Case VarType(TrimChars)
Case vbString
mTrimChars.SA.pvData = StrPtr(TrimChars)
mTrimChars.SA.cElements = Len(TrimChars)
StartIndex = TrimStartIndex(s, mTrimChars.Data)
Case vbIntegerArray
SAPtr(Chars) = GetArrayPointer(TrimChars, True)
StartIndex = TrimStartIndex(s, Chars)
SAPtr(Chars) = 0
Case vbError
StartIndex = SzTrimStartIndex(s)
Case Else
Throw Cor.NewArgumentException("Invalid character set.")
End Select
If StartIndex = 0 Then Exit Function
TrimStart = Mid$(s, StartIndex)
End Function
''
' Trims the end of the string.
'
' @param s The string to be trimmed.
' @param trimChars The characters to remove.
' @return The trimmed string.
' @remarks Unlike VB's RTrim function, this can trim multiple character types,
' not just spaces.
' <p>The <i>trimChars</i> can be either a string of the characters or an integer array
' of characters that will be trimmed from the end.</p>
'
Public Function TrimEnd(ByRef s As String, Optional ByRef TrimChars As Variant) As String
Dim Chars() As Integer
Dim EndIndex As Long
Select Case VarType(TrimChars)
Case vbString
mTrimChars.SA.pvData = StrPtr(TrimChars)
mTrimChars.SA.cElements = Len(TrimChars)
EndIndex = TrimEndIndex(s, mTrimChars.Data)
Case vbIntegerArray
SAPtr(Chars) = GetArrayPointer(TrimChars, True)
EndIndex = TrimEndIndex(s, Chars)
SAPtr(Chars) = 0
Case vbError
EndIndex = SzTrimEndIndex(s)
Case Else
Throw Cor.NewArgumentException("Invalid character set.")
End Select
If EndIndex = 0 Then Exit Function
TrimEnd = Left$(s, EndIndex)
End Function
''
' Trims both the beginning and end of the string.
'
' @param s The string to be trimmed.
' @param trimChars The characters to remove.
' @return The trimmed string.
' @remarks Unlike VB's Trim function, this can trim multiple character types,
' not just spaces.
' <p>The <i>trimChars</i> can be either a string of the characters or an integer array
' of characters that will be trimmed from both ends.</p>
'
Public Function Trim(ByRef s As String, Optional ByRef TrimChars As Variant) As String
Dim Chars() As Integer
Dim StartIndex As Long
Dim EndIndex As Long
Select Case VarType(TrimChars)
Case vbString
mTrimChars.SA.pvData = StrPtr(TrimChars)
mTrimChars.SA.cElements = Len(TrimChars)
StartIndex = TrimStartIndex(s, mTrimChars.Data)
EndIndex = TrimEndIndex(s, mTrimChars.Data)
Case vbIntegerArray
SAPtr(Chars) = GetArrayPointer(TrimChars, True)
StartIndex = TrimStartIndex(s, Chars)
EndIndex = TrimEndIndex(s, Chars)
SAPtr(Chars) = 0
Case vbError ' assume missing
StartIndex = SzTrimStartIndex(s)
EndIndex = SzTrimEndIndex(s)
Case Else
Throw Cor.NewArgumentException("Invalid character set.")
End Select
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -