在 VB6 中,Trim() 函数修剪字符串前后的空格。 我想知道是否有一个函数不仅可以修剪空格,还可以修剪字符串两端的所有空格(在本例中为制表符)。
您必须将
Trim
函数与 Replace
函数结合起来:
s = " ABC " & vbTab & " "
MsgBox Len(s)
MsgBox Len(Trim$(s))
s = Replace$(Trim$(s), vbTab, "")
MsgBox Len(s)
注意: 上面的代码还将删除嵌入的选项卡。可能可以使用正则表达式解决此问题,但这里有一种通过循环仅从末尾修剪空格/制表符的方法:
Dim s As String, char As String, trimmedString As String
Dim x As Integer
s = " " & vbTab & " ABC " & vbTab & "a " & vbTab
'// Trim all spaces/tabs from the beginning
For x = 1 To Len(s)
char = Mid$(s, x, 1)
If char = vbTab Or char = " " Then
Else
trimmedString = Mid$(s, x)
Exit For
End If
Next
'// Now do it from the end
For x = Len(trimmedString) To 1 Step -1
char = Mid$(trimmedString, x, 1)
If char = vbTab Or char = " " Then
Else
trimmedString = Left$(trimmedString, x)
Exit For
End If
Next
你最终应该得到
ABC{space}{space}{tab}a
我使用这个功能:
Private Function TrimAll(Text As String) As String
Const toRemove As String = " " & vbTab & vbCr & vbLf 'what to remove
Dim s As Long: s = 1
Dim e As Long: e = Len(Text)
Dim c As String
If e = 0 Then Exit Function 'zero len string
Do 'how many chars to skip on the left side
c = Mid(Text, s, 1)
If c = "" Or InStr(1, toRemove, c) = 0 Then Exit Do
s = s + 1
Loop
Do 'how many chars to skip on the right side
c = Mid(Text, e, 1)
If e = 1 Or InStr(1, toRemove, c) = 0 Then Exit Do
e = e - 1
Loop
TrimAll = Mid(Text, s, (e - s) + 1) 'return remaining text
End Function
用途:
Debug.Print "|" & TrimAll("") & "|" 'prints ||
Debug.Print "|" & TrimAll(" ") & "|" 'prints ||
Debug.Print "|" & TrimAll("a") & "|" 'prints |a|
Debug.Print "|" & TrimAll("a ") & "|" 'prints |a|
Debug.Print "|" & TrimAll(" a") & "|" 'prints |a|
Debug.Print "|" & TrimAll(" a b ") & "|" 'prints |a b|
Debug.Print "|" & TrimAll(vbTab & " " & "Some " & vbCrLf & " text. " & vbCrLf & " ") & "|" 'prints |Some
text.|
您只需在 toRemove 字符串中添加要删除的字符即可。
它不会一次又一次地复制部分修剪过的字符串,而是搜索修剪过的字符串的开始和结束位置,并仅返回该部分。
怎么样:
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenW" ( _
ByVal lpString As Long) As Long
Private Declare Function StrTrim Lib "shlwapi" Alias "StrTrimW" ( _
ByVal pszSource As Long, _
ByVal pszTrimChars As Long) As Long
Private Function TrimWS(ByVal Text As String) As String
'Unicode-safe.
Const WHITE_SPACE As String = " " & vbTab & vbCr & vbLf
If StrTrim(StrPtr(Text), StrPtr(WHITE_SPACE)) Then
TrimWS = Left$(Text, lstrlen(StrPtr(Text)))
Else
TrimWS = Text
End If
End Function
速度很快,如果您使用类型库而不是
Declare
来定义 API 调用,甚至会更快。
遗憾的是没有内置功能。 这是我写的。 它确实有效。
Function TrimAllWhitespace(ByVal str As String)
str = Trim(str)
Do Until Not Left(str, 1) = Chr(9)
str = Trim(Mid(str, 2, Len(str) - 1))
Loop
Do Until Not Right(str, 1) = Chr(9)
str = Trim(Left(str, Len(str) - 1))
Loop
TrimAllWhitespace = str
End Function
这也可能有用,@MathewHagemann 的继续 它删除前后的空行
Public Function TrimAllWhitespace(ByVal str As String)
str = Trim(str)
Do Until Not Left(str, 1) = Chr(9)
str = Trim(Mid(str, 2, Len(str) - 1))
Loop
Do Until Not Right(str, 1) = Chr(9)
str = Trim(Left(str, Len(str) - 1))
Loop
Do Until Not Left(str, 1) = Chr(13)
str = Trim(Mid(str, 2, Len(str) - 1))
Loop
Do Until Not Left(str, 1) = Chr(10)
str = Trim(Mid(str, 2, Len(str) - 1))
Loop
Do Until Not Right(str, 1) = Chr(10)
str = Trim(Left(str, Len(str) - 1))
Loop
Do Until Not Right(str, 1) = Chr(13)
str = Trim(Left(str, Len(str) - 1))
Loop
TrimAllWhitespace = str
End Function
这是我想出的东西,可以让您选择返回修剪后的字符串本身或修剪后的字符串的长度
在模块中
'=========================================================
'this function lets get either the len of a string with spaces and tabs trimmed of
'or get the string itself with the spaces and tabs trimmed off
'=========================================================
Public Property Get eLen(sStr As String, Optional bTrimTabs As Boolean = True, Optional bReturnLen As Boolean = True) As Variant
'function which trims away spaces and tabs (if [bTrimTabs] is set to True)
Dim s As String: s = sfuncTrimEnds(sStr, bTrimTabs)
If bReturnLen Then ' if [bReturnLen] = True then return the trimmed string len
eLen = Len(s)
Else ' if [bReturnLen] = False then return the trimmed string
eLen = s
End If
End Property
'===============================================================
' this function trims spaces from both sides of string and tabs if [bTrimTabs] = true
' the return value is the string with the spaces (and tabs) trimmed off both sides
'===============================================================
Private Function sfuncTrimEnds(ByVal sStr As String, Optional bTrimTabs As Boolean = True) As String
Dim lStart As Long, lEnd As Long
Dim sChr As String
Dim llen As Long: llen = Len(sStr)
Dim l As Long: For l = 1 To llen
sChr = Mid$(sStr, l, 1)
If sChr <> " " And sChr <> vbTab Then
lStart = l
Exit For
End If
Next l
For l = llen To 1 Step -1
sChr = Mid$(sStr, l, 1)
If sChr <> " " And sChr <> vbTab Then
lEnd = l
Exit For
End If
Next l
sStr = Mid$(sStr, lStart, (lEnd - (lStart - 1)))
sfuncTrimEnds = sStr
End Function
要使用这个:
Dim s As String: s = " " & vbTab & " " & "mary wants my little lamb " & " " & vbTab & " "
MsgBox Tru.eLen(s, , False) 'will return the trimmed text
MsgBox Tru.eLen(s) ' will return the len of the trimmed text
或
Dim sVal As String: sVal = Tru.eLen(s, , False)
if len(sval) > 0 then ' yada yada
对于 vb.net(非常相似),从前面删除所有空格和控制字符:
Public Function TrimWspFromFront(ByRef MyStr As String) As String
While MyStr.Length > 0 AndAlso Left(MyStr, 1) < " "
MyStr = Trim(Right(MyStr, MyStr.Length - 1))
End While
Return MyStr
End Function
从后面拆下:
Public Function TrimWspFromEnd(ByRef MyStr As String) As String
While MyStr.Length > 0 AndAlso Right(MyStr, 1) < " "
MyStr = Trim(Left(MyStr, MyStr.Length - 1))
End While
Return MyStr
End Function
注意。作为 ByRef 传递以避免制作副本的开销,其他人可能更喜欢编码为 Sub 或传递 ByVal
最好不要忘记迭代函数本身,因为可能存在一系列未排序的制表符、空格和换行符,并且您希望清除所有这些。
“制表符 & 空格 & 制表符 & 制表符 & 换行符 & 空格 & 制表符 & 换行符 ....”
这是另一种由 3 个函数组成的方法:
前2个功能可以独立使用。 第三个函数 (TrimAll) 演示如何组合前两个函数以包含要从字符串中修剪的任何自定义字符。
TrimWS: 删除所有前导/尾随空格和制表符。
Function TrimWS(ByVal str)
Do
str = Trim(str)
If Left(str, 1) = vbTab Then str = Mid(str, 2)
If Right(str, 1) = vbTab Then str = Left(str, Len(str)-1)
Loop While (Left(str, 1)=" ") Or (Right(str, 1)=" ")
TrimWS = str
End Function
TrimChar: 删除所有等于作为参数传递的字符的前导/尾随字符。
Function TrimChar(ByVal str, ByVal char)
Do While (Left(str, 1)=char) And (Right(str, 1)=char)
str = Mid(str, 2, Len(str)-2)
Loop
TrimChar = str
End Function
TrimAll: 删除所有前导/尾随空格、制表符和指定的任何其他字符。在下面的示例中,它将从字符串中去除所有空格、制表符、双引号 (") 和单引号 (')。
注意: 如果您要在 TrimAll 中添加或删除字符,请记住相应地修改行 Dim Flag(3) As Integer。
Function TrimAll(ByVal str)
Dim OldValue As String
Dim NewValue As String
Dim Flag(3) As Integer
Dim x As Integer
Do
Flag(0) = 0
OldValue = str : NewValue = TrimWS(str)
If OldValue = NewValue Then Flag(1) = 0 Else Flag(1) = 1 : str = NewValue
OldValue = str : NewValue = TrimChar(str, """")
If OldValue = NewValue Then Flag(2) = 0 Else Flag(2) = 1 : str = NewValue
OldValue = str : NewValue = TrimChar(str, "'")
If OldValue = NewValue Then Flag(3) = 0 Else Flag(3) = 1 : str = NewValue
For x = 1 To UBound(Flag)
Flag(0) = Flag(0) + Flag(x)
Next
Loop Until Flag(0)=0
TrimAll = str
End Function
我已将之前答案中的函数组合成一个函数,该函数将从字符串中删除任何前导和/或尾随空格(空格和制表符),以及可选地在函数的第二个参数中传递的任何字符。
要仅修剪空格,请将空字符串传递到第二个参数。
我经常使用此函数通过调用如下函数来修剪包含文件或文件夹路径的字符串:
NewValue = Clip(FilePath, """'")
上面的示例将去除任何前导或尾随空格,但仅去除平衡双引号或单引号。这意味着仅当字符串以 1 开头和结尾时,引号才会被修剪。
这是函数:
Private Function Clip(ByVal strString, ByVal strChars) 'As String
Dim x, y
Dim NewString, Char
ReDim Flag(Len(strChars)+1)
Do
NewString=strString : Flag(0)=0
Do
NewString = Trim(NewString)
If Left(NewString, 1) = vbTab Then NewString = Mid(NewString, 2)
If Right(NewString, 1) = vbTab Then NewString = Left(NewString, Len(NewString) - 1)
Loop While (Left(NewString, 1) = " ") Or (Right(NewString, 1) = " ")
If strString=NewString Then Flag(1)=0 Else Flag(1)=1 : strString=NewString
For y = 1 To Len(strChars)
NewString=strString : Char=Mid(strChars, y, 1)
Do While (Left(NewString, 1) = Char) And (Right(NewString, 1) = Char)
NewString = Mid(NewString, 2, Len(NewString) - 2)
Loop
If strString=NewString Then Flag(y+1)=0 Else Flag(y+1)=1 : strString=NewString
Next
For x = 1 To UBound(Flag)
Flag(0) = Flag(0) + Flag(x)
Next
Loop Until Flag(0)=0
Clip = strString
End Function 'Clip
XL 电池的新生产线存在一些问题,变得更加通用
Private Function TrimAll(Text As String) As String
Dim s As Long: s = 1
Dim e As Long: e = Len(Text)
Dim c As String
If e = 0 Then Exit Function 'zero len string
For s = 1 To e
If Mid(Text, s, 1) <= " " Then c = c & " "
While s < e And Mid(Text, s, 1) <= " "
s = s + 1
Wend
c = c & Mid(Text, s, 1)
Next
TrimAll = c
End Function
请注意,这个简单的函数不会修剪字符串的开头或结尾,但现在甚至连制表符都被替换了(XL 将它们假冒为空格),硬空间也未测试(可能会>32)。
Const cXTrim As String = " " & vbTab & vbCr & vbLf & vbVerticalTab & vbFormFeed & vbNullChar
Public Function XLTrim(sText) As String
Dim i As Integer
For i = 1 To Len(sText)
If InStr(cXTrim, Mid(sText, i, 1)) = 0 Then Exit For
Next
XLTrim = Mid$(sText, i)
End Function
Public Function XRTrim(sText) As String
Dim i As Integer
For i = Len(sText) To 1 Step -1
If InStr(cXTrim, Mid(sText, i, 1)) = 0 Then Exit For
Next
XRTrim = Mid$(sText, 1, i)
End Function
Public Function XTrim(sText) As String
Dim i As Integer
Dim k As Integer
For i = 1 To Len(sText)
If InStr(cXTrim, Mid(sText, i, 1)) = 0 Then Exit For
Next
For k = Len(sText) To 1 Step -1
If InStr(cXTrim, Mid(sText, k, 1)) = 0 Then Exit For
Next
XTrim = Mid$(sText, i, k - i + 1)
End Function
Public Function XTrim(sText) As String 'alternative
XTrim = XLTrim(XRTrim(sText))
End Function
我能想到的最简单的解决方案,也可以使用空字符串,但这里的一些示例不这样做。