Option Explicit Dim strP As StringPrivate Sub Command1_Click() Dim intP As Integer Dim strA() As String strA = Split(strP, " ") Text1.Text = "" For intP = LBound(strA) To UBound(strA) Text1.Text = Text1.Text & strA(intP) Next intP End SubPrivate Sub Form_Load() strP = "A B C D E" End Sub
strp=Replace(strp, " ", "")
本帖最后由 bcrun 于 2010-10-09 11:18:04 编辑
你如果把字符串中的空格替掉之后,那拿什么来区分ABCDE呢
Private Sub Form_Load()Dim a, b Dim i As Integer a = "A B CD E" b = Replace(a, " ", "") For i = 1 To Len(b) a = Mid(b, i, 1) Next i
End Sub
while instr(strp," ")>0 strp=Replace(strp, " ", " ") wend
Option Explicit Dim strP As StringPrivate Sub Command1_Click() Dim intP As Integer Dim strA() As String dim s As String strA = Split(strP, " ") For intP = LBound(strA) To UBound(strA) if strA(intP)<>"" then s=s+"["+strA(intP)+"]" Next intP msgbox s End SubPrivate Sub Form_Load() strP = "A B C D E" End Sub
先用replace删除掉多于的空格,只保留一个空格作为间隔符,然后再使用Split拆分。 比如:Sub main() Dim i As Integer Dim s As String Dim arr() As String
s = "a b c d"
'过滤掉多余的空格 Do s = Replace(s, " ", " ") Loop Until InStr(s, " ") = 0 '拆分并显示结果 arr = Split(s, " ") For i = 0 To UBound(arr) Debug.Print i, arr(i) Next End Sub
'*****读取文件表头信息******************************************** Sub ReadFileInfo() Open MngBaseFileName For Input As #intFilenum Dim a() As String Line Input #intFilenum, strline Deltblank '转到 Deltblank 去除多余空格 a = Split(strline) v01000 = a(0) v04001 = a(6) v04002 = a(7) Close #intFilenum End Sub '*****去除多余的空格******************************************** Sub Deltblank() Do If InStr(1, strline, " ", vbTextCompare) = 0 Then Exit Do strline = Replace(strline, " ", " ") Loop End Sub 这是我常用的方法 很管用,如果格式里面还有TAB格式的话可以先替换成空格 '*****去除多余的空格******************************************** Sub Deltblank() strline = Replace(Trim(strline), Chr(9), " ") Do If InStr(1, strline, " ", vbTextCompare) = 0 Then Exit Do strline = Replace(strline, " ", " ") Loop End Sub
用正则将多个空格替换成一个,然后分割;或者只取非空数据。Sub GetStr() Dim oJs As Object,Str$ Str = "A B C D E F G" Set oJS = CreateObject("ScriptControl"):oJS.Language = "JScript" Ojs.eval "function gets{return str.match(/\S+/g,'')}" Str = oJs.Codeobject.gets(Str) Debug.Print Str End Sub
Private Sub Command1_Click() Dim N As String, i As Integer Dim R() As Integer
N = "123 33 55 66 77" While InStr(N, " ") ReDim Preserve R(i) R(i) = Mid(N, 1, InStr(N, " ") - 1) N = Trim(Mid(N, InStr(N, " "))) i = i + 1 Wend ReDim Preserve R(i) R(i) = N
Option Explicit
Dim strP As StringPrivate Sub Command1_Click()
Dim intP As Integer
Dim strA() As String
strA = Split(strP, " ")
Text1.Text = ""
For intP = LBound(strA) To UBound(strA)
Text1.Text = Text1.Text & strA(intP)
Next intP
End SubPrivate Sub Form_Load()
strP = "A B C D E"
End Sub
Private Sub Form_Load()Dim a, b
Dim i As Integer a = "A B CD E"
b = Replace(a, " ", "")
For i = 1 To Len(b)
a = Mid(b, i, 1)
Next i
End Sub
strp=Replace(strp, " ", " ")
wend
Dim strP As StringPrivate Sub Command1_Click()
Dim intP As Integer
Dim strA() As String
dim s As String
strA = Split(strP, " ")
For intP = LBound(strA) To UBound(strA)
if strA(intP)<>"" then s=s+"["+strA(intP)+"]"
Next intP
msgbox s
End SubPrivate Sub Form_Load()
strP = "A B C D E"
End Sub
比如:Sub main()
Dim i As Integer
Dim s As String
Dim arr() As String
s = "a b c d"
'过滤掉多余的空格
Do
s = Replace(s, " ", " ")
Loop Until InStr(s, " ") = 0 '拆分并显示结果
arr = Split(s, " ")
For i = 0 To UBound(arr)
Debug.Print i, arr(i)
Next
End Sub
Sub ReadFileInfo()
Open MngBaseFileName For Input As #intFilenum
Dim a() As String
Line Input #intFilenum, strline
Deltblank '转到 Deltblank 去除多余空格
a = Split(strline)
v01000 = a(0)
v04001 = a(6)
v04002 = a(7)
Close #intFilenum
End Sub
'*****去除多余的空格********************************************
Sub Deltblank()
Do
If InStr(1, strline, " ", vbTextCompare) = 0 Then Exit Do
strline = Replace(strline, " ", " ")
Loop
End Sub
这是我常用的方法
很管用,如果格式里面还有TAB格式的话可以先替换成空格
'*****去除多余的空格********************************************
Sub Deltblank()
strline = Replace(Trim(strline), Chr(9), " ")
Do
If InStr(1, strline, " ", vbTextCompare) = 0 Then Exit Do
strline = Replace(strline, " ", " ")
Loop
End Sub
Dim oJs As Object,Str$
Str = "A B C D E F G" Set oJS = CreateObject("ScriptControl"):oJS.Language = "JScript"
Ojs.eval "function gets{return str.match(/\S+/g,'')}" Str = oJs.Codeobject.gets(Str)
Debug.Print Str
End Sub
Private Sub Command1_Click()
Dim N As String, i As Integer
Dim R() As Integer
N = "123 33 55 66 77"
While InStr(N, " ")
ReDim Preserve R(i)
R(i) = Mid(N, 1, InStr(N, " ") - 1)
N = Trim(Mid(N, InStr(N, " ")))
i = i + 1
Wend
ReDim Preserve R(i)
R(i) = N
End Sub