在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
结尾