public Function TrimString(P_str as string) as string dim I,Num as Integer dim strTemp as String dim strR as String Num=len(P_str) for I=1 to Num strTemp=mid(P_str,i,1) if strTemp<>" " then strR=strR & strTemp endif next I TrimString=Trim(strR) end function
都没考虑多个空格的情况! VB考题: 如何将某字符串中的多个空格压缩为一个空格(如:" 啊 ac B " 变为 " 啊 ac B ")? http://www.csdn.net/expert/topic/804/804122.xml?temp=.2836573
Private Sub Command1_Click() Dim a As String a = "aaaa bbbb cccc dddd " Form1.Print a MyTrim a Form1.Print a End Sub Public Function MyTrim(ByRef TempStr As String) As Boolean Dim Str1() As String Dim I As Long Dim TestStr As String Str1 = Split(TempStr, " ") For I = LBound(Str1) To UBound(Str1) TestStr = TestStr & Trim(Str1(I)) Next I TempStr = TestStr End Function
小干部的提问,如下回答 Private Sub Command1_Click() Dim a As String a = "aaaa bbbb cccc dddd " Form1.Print a MyTrim a Form1.Print a End Sub Public Function MyTrim(ByRef TempStr As String) As Boolean Dim Str1() As String Dim I As Long Dim TestStr As String Str1 = Split(TempStr, " ") For I = LBound(Str1) To UBound(Str1) TestStr = TestStr & Trim(Str1(I)) &" " Next I TempStr = TestStr End Function
Join解决不了问题,跟没做一样
strReturn = Replace(strOrg, " ", vbNullString)
Private Sub Command1_Click() Dim bStr As String 'bStr中有空格 Dim aStr As String 'aStr为去除空格后的字符 Dim Str1 As String Dim Str2 As String bStr = "qq er" '假设bStr中有空格 N = InStr(a, " ") 'N为所查找的第一个空格的位置 Str1 = Left(bStr, (N - 1)) Str2 = Right(bStr, Len(bStr) - N) aStr = Trim(Str1) + Trim(Str2) '去除Str1和Str2前后的字符并合并成aStr MsgBox bStr & Space(5) & aStr '弹出字符去除空格前后的情形。 End Sub
Function aStr(bStr As String) For k = 1 To Len(bStr) i = InStr(bStr, " ") If i > 1 Then bStr = Left(bStr, i - 1) & Right(bStr, Len(bStr) - i) Else bStr = Trim(bStr) End If Next k aStr = bStr End Function '上面的是去除只有中间有空格的程序过程啊!所以我又另外编了一个函数。 '这个函数可以用来去除字符串中的前、中、后的所有空格,谢谢调试!
Private Sub Command1_Click() Dim x Dim a Dim i Dim c
a = "S30813-Q 55-X 2 " x = Split(a, " ") For i = LBound(x) To UBound(x) c = c + x(i) Next i Debug.Print c End Sub
str1 = replace(str1, " ", "") '对VB5.0不行6.0可以
dim I,Num as Integer
dim strTemp as String
dim strR as String Num=len(P_str)
for I=1 to Num
strTemp=mid(P_str,i,1)
if strTemp<>" " then
strR=strR & strTemp
endif
next I
TrimString=Trim(strR)
end function
VB考题: 如何将某字符串中的多个空格压缩为一个空格(如:" 啊 ac B " 变为 " 啊 ac B ")?
http://www.csdn.net/expert/topic/804/804122.xml?temp=.2836573
Dim a As String
a = "aaaa bbbb cccc dddd "
Form1.Print a
MyTrim a
Form1.Print a
End Sub
Public Function MyTrim(ByRef TempStr As String) As Boolean
Dim Str1() As String
Dim I As Long
Dim TestStr As String
Str1 = Split(TempStr, " ")
For I = LBound(Str1) To UBound(Str1)
TestStr = TestStr & Trim(Str1(I))
Next I
TempStr = TestStr
End Function
Private Sub Command1_Click()
Dim a As String
a = "aaaa bbbb cccc dddd "
Form1.Print a
MyTrim a
Form1.Print a
End Sub
Public Function MyTrim(ByRef TempStr As String) As Boolean
Dim Str1() As String
Dim I As Long
Dim TestStr As String
Str1 = Split(TempStr, " ")
For I = LBound(Str1) To UBound(Str1)
TestStr = TestStr & Trim(Str1(I)) &" "
Next I
TempStr = TestStr
End Function
Dim bStr As String 'bStr中有空格
Dim aStr As String 'aStr为去除空格后的字符
Dim Str1 As String
Dim Str2 As String
bStr = "qq er" '假设bStr中有空格
N = InStr(a, " ") 'N为所查找的第一个空格的位置
Str1 = Left(bStr, (N - 1))
Str2 = Right(bStr, Len(bStr) - N)
aStr = Trim(Str1) + Trim(Str2) '去除Str1和Str2前后的字符并合并成aStr
MsgBox bStr & Space(5) & aStr '弹出字符去除空格前后的情形。
End Sub
For k = 1 To Len(bStr)
i = InStr(bStr, " ")
If i > 1 Then
bStr = Left(bStr, i - 1) & Right(bStr, Len(bStr) - i)
Else
bStr = Trim(bStr)
End If
Next k
aStr = bStr
End Function
'上面的是去除只有中间有空格的程序过程啊!所以我又另外编了一个函数。
'这个函数可以用来去除字符串中的前、中、后的所有空格,谢谢调试!
Dim x
Dim a
Dim i
Dim c
a = "S30813-Q 55-X 2 "
x = Split(a, " ")
For i = LBound(x) To UBound(x)
c = c + x(i)
Next i
Debug.Print c
End Sub