(求教)文本中的数据个数
........................
.......................
0.231-1.29-2.848-3.263-4.159-3.224-1.95
0.0441.2922.4382.0180.502-0.8682.065
3.0930.168-1.055-0.414-2.132-4.681-2.4
1.445-0.5511.2081.7541.4080.1270.366
1.4611.7250.9370.615-0.281-1.986-3.428
0.015-0.026-1.451-0.4281.5961.5460.394
3.364.7674.562.9880.211-2.509-2.908
-1.29-0.953-1.249-1.998-3.908-3.1310.142
4.6714.8344.7274.6992.930.644-0.776
-2.223-0.0970.912-1.308-4.855-3.859-0.508
0.0132.6983.4913.3121.5660.5751.137
0.299-2.998-5.818
我用split和UBound只得出了列和行的个数,而我的这些数据最后一行的个数是变换的,比如我上面列出的例子,怎样才能读出总的个数,数据中间是空格,这些数据会有几千个。
[解决办法]
function numcount(byval s as string) as long
dim count as long
dim ch as string
dim isNum as boolean
dim i as long
for i=1 to len(s)
ch = mid$(s,i,1)
if instr(1, "0123456789.+- ", ch) <> 0 then
if not isNum then count = count+1
isNum = true
else
isNum = false
end if
next
numcount = count
end function
[解决办法]
Function GetCount(pFile As String) As Long
'pFile文本路径和名称 如: "C:\test.txt "
Dim hFile As Integer
Dim sFile As String
Dim arr() As String
Dim i As Integer
hFile = FreeFile()
Open pFile For Binary As hFile
sFile = Space(LOF(hFile))
Get hFile, , sFile
Close hFile
sFile = Replace(sFile, vbCrLf, Chr(32))
sFile = Replace(sFile, Chr(9), Chr(32))
arr = Split(sFile, Chr(32))
For i = 0 To UBound(arr)
If Trim(arr(i)) <> " " Then GetCount = GetCount + 1
Next
End Function