回复 5# 的帖子
'哪有这么多时间一个个把代码贴出来啊 直接给思路好些
'既然有要求了,还是给个代码吧
'把下面的代码放到一个标准模块里
Option Explicit
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const EM_GETLINECOUNT = &HBA
'读取文本流
Public Function getTextStream(FileName As String) As String
On Error GoTo ErrorHand
Dim TempStr As String
Dim FileNum As Long
FileNum = FreeFile
Open FileName For Input As #FileNum
TempStr = StrConv(InputB(LOF(FileNum), #1), vbUnicode)
Close #FileNum
getTextStream = TempStr
Exit Function
ErrorHand:
getTextStream = ""
End Function
'方法1,此方法直接搜索字符串,比较通用
Public Function getStringCount(ByVal SearchIn As String, ByVal SearchFor As String) As Long
Dim Number As Long
Dim Pos
As Long
Pos = InStr(1, SearchIn, SearchFor)
Do While Pos <> 0
Number = Number + 1
Pos = InStr(Pos + 1, SearchIn, SearchFor)
Loop
getStringCount = Number
End Function
'方法2,此方法用Split,比方法1高效
Public Function getCrlfCount(ByVal SearchString As String) As Long
Dim ret As Long
Dim StrArray() As String
StrArray = Split(SearchString, Chr(10))
ret = UBound(StrArray)+1
getCrlfCount = ret
End Function
'方法3,此方法统计TextBox数据行数
Public Function GetTextLines(txtHwnd As Long) As Long
GetTextLines = SendMessage(txtHwnd, EM_GETLINECOUNT, 0, 0)
End Function
'测试,在C盘建立num.txt文件,内容如下:
当前密码:000905
200001
400001
600001
800000
当前密码:001105
200001
400001
600001
800000
结束密码:001921
200001
400001
600001
800000
测试结果:
方法1:print getstringcount(gettextstream("C:\num.txt"),chr(10))
结果:3
方法2:print getcrlfcount(gettextstream("C:\num.txt"))
结果:3
[[it] 本帖最后由 三断笛 于 2008-11-7 20:46 编辑 [/it]]