在VB6中,Trim()函数修剪字符串前后的空格。我想知道是否有一个函数不仅会修剪空格,而是修剪字符串每一端的所有空格(在本例中为制表符)。
答案 0 :(得分:7)
您必须将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
答案 1 :(得分:3)
怎么样:
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调用,则速度更快,甚至更快。
答案 2 :(得分:2)
遗憾的是没有内置功能。这是我写的那个。这样做。
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
答案 3 :(得分:2)
我使用这个功能:
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字符串中添加要删除的字符。
它不会一次又一次地复制部分修剪的字符串,而是搜索修剪后的字符串开始和结束的位置,并仅返回该部分。
答案 4 :(得分:1)
这也很有用,是@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
答案 5 :(得分:1)
这是我想出的一些东西,让你可以选择返回修剪过的字符串本身还是修剪过的字符串的长度
在模块中
'=========================================================
'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
OR
Dim sVal As String: sVal = Tru.eLen(s, , False)
if len(sval) > 0 then ' yada yada
答案 6 :(得分:1)
对于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
NB。通过ByRef传递以避免制作副本的开销,其他人可能更喜欢编码为Sub或传递ByVal
答案 7 :(得分:0)
最好不要忘记迭代自身的功能,因为可能会有一系列选项卡空白和换行符以无序的方式出现,而您希望清理所有这些功能。
“制表符和空格和制表符和换行符与空格和制表符和折线....”
答案 8 :(得分:0)
这是另一种由 3 个函数组成的方法:
前两个函数可以独立使用。 第三个函数 (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
答案 9 :(得分:0)
我已将上一个答案中的函数组合到一个函数中,该函数将从字符串中修剪任何前导和/或尾随空格(空格和制表符)以及可选的任何在函数的第二个参数中传递的字符。< /p>
要仅修剪空格,请将空字符串传递给第二个参数。
我经常使用这个函数通过调用这样的函数来修剪包含文件或文件夹路径的字符串:
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