请教:txt文本中字符串操作的问题,谢谢!

CACACAW 2003-07-30 10:24:56
例如test.txt内容如下:

aa bdooufwfwef
bbb efrgfhytj6j
c ergrthrh
aa ohoijoih

查找text.txt里有多少行以“aa”开头的行,
并取出该行aa后面的字符串,字符串头尾不含有空格。
...全文
71 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zmrok 2003-07-30
  • 打赏
  • 举报
回复
这些东西,查查书就知道了
hc_z 2003-07-30
  • 打赏
  • 举报
回复
从文件一行一行读出来,在找
if instr(trim(str),"aa")=0 then'找到
Surpass 2003-07-30
  • 打赏
  • 举报
回复
把字符串写在哪里呀?文件还是文本框?
给你一些例子吧,没有时间整理,可以举一反三。
□ 怎样取得一个字符串在另外一个字符串中出现的次数?
Public Function sCount(String1 As String, String2 As String) As Integer
Dim I As Integer, iCount As Integer
I = 1
Do
If (I > Len(String1)) Then Exit Do
I = InStr(I, String1, String2, vbTextCompare)
If I Then
iCount = iCount + 1
I = I + 2
DoEvents
End If
Loop While I
sCount = iCount
End Function


□ 怎样在一个字符串中完全删除里面的另外一个字符串?
Function StringCleaner(s As String, _
Search As String) As String
Dim i As Integer, res As String
res = s
Do While InStr(res, Search)
i = InStr(res, Search)
res = Left(res, i - 1) & _
Mid(res, i + 1)
Loop
StringCleaner = res
End Function

□ 怎样在一个字符串中删除里面的另外一个字符串?
Public Sub sRemove(String1 As String, String2 As String)
Dim I As Integer
I = 1
Do
If (I > Len(String1)) Then Exit Do
I = InStr(I, String1, String2)
If I Then
String1 = Left$(String1, I - 1) + Mid$(String1, I + Len(String2)+1)
I = I + 2
DoEvents
End If
Loop While I
End Sub

□ 怎样在一个字符串中替换里面的另外一个字符串?
Public Sub sReplace(String1 As String, String2 As String, RepString As String)
Dim I As Integer
I = 1
Do
If (I > Len(String1)) Then Exit Do
I = InStr(I, String1, String2)
If I Then
String1 = Left$(String1, I - 1) + RepString + Mid$(String1, I + Len(String2) )
I = I + 2
DoEvents
End If
Loop While I
End Sub

□ 如何计算一个字符串中的行数?
Function CountStringLine(src_string As String) As Integer
On Error Resume Next
Dim string_flag As Integer
Dim line_cnt As Integer
Dim test_string As String
line_cnt = 0 '初始--> 行数为1
string_flag = 1 '标志为1
test_string = src_string
DoEvents
Do
line_cnt = line_cnt + 1
string_flag = InStr(test_string, vbCrLf) ’判断回车换行
test_string = Right(test_string, Len(test_string) - string_flag - 1)
Loop Until string_flag <= 0
CountStringLine = line_cnt
End Function

□ 如何从一个字符串中读取一行字符?
Function ReadStringLine(src_str As String, lineno As Integer) As String
On Error Resume Next
Dim string_flag As Integer
Dim line_cnt As Integer
Dim test_string As String
Dim ret_string As String
line_cnt = 0 '初始--> 行数为1
string_flag = 1 '标志为1
test_string = Right(src_str, 2)
If test_string <> vbCrLf Then
test_string = src_str + vbCrLf
Else
test_string = src_str
End If
DoEvents
Do
line_cnt = line_cnt + 1
string_flag = InStr(test_string, vbCrLf)
ret_string = Left(test_string, string_flag)
test_string = Right(test_string, Len(test_string) - string_flag - 1)
Loop Until lineno <= line_cnt
'If line_cnt = 1 Then
' ReadStringLine = ret_string
'Else
ReadStringLine = Left(ret_string, Len(ret_string) - 1)
'End If
End Function
lxqlogo0 2003-07-30
  • 打赏
  • 举报
回复
open "c:\test.txt" for input as #1
while not eof(1)
line input #1,txt
if instr(1,txt,"aa",vbTextCompare)<>0 then
i=i+1
txt=trim(mid(txt,3))
debug.print txt
end if

wend
debug.print i
close #1
lxqlogo0 2003-07-30
  • 打赏
  • 举报
回复
open "c:\test.txt" for input as #1
while not eof(1)
line input #1,txt
if instr(1,txt,"aa",vbTextCompare)<>0 then
i=i+1
txt=trim(mid(txt,3))
debug.print txt
end if

wend
debug.print i
close #1
victorycyz 2003-07-30
  • 打赏
  • 举报
回复
这个不难吧,先贴出你的程序段,看看你哪里不会吧。
aalei 2003-07-30
  • 打赏
  • 举报
回复
强烈推荐
open "c:\test.txt" for input as #1
while not eof(1)
line input #1,txt
if instr(1,txt,"aa",vbTextCompare)<>0 then
i=i+1
txt=trim(mid(txt,3))
debug.print txt
end if

wend
debug.print i
close #1
CACACAW 2003-07-30
  • 打赏
  • 举报
回复
那位老大给出完整的代码,我再去依次翻书

7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧