文本文件中,如何查寻字符串,及插入另一字符串?

sslijun 2003-11-17 03:31:45
文本文件中,如何查寻字符串,及插入另一字符串?
例如:file.txt内容如下:
123
abc
789
123
456
888
abc
777
.
.
.

在 file.txt 文件中,首先搜寻字符串abc 定其行的位置, 再搜寻abc 行后面的字符串123,在其后面一行插入另一个字符串 ccc.
即:在 123 和 456 之间 插入字符串 ccc.

请问编程如何简单快速实现?
谢谢
...全文
74 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rainstormmaster 2003-11-17
  • 打赏
  • 举报
回复
//在 file.txt 文件中,首先搜寻字符串abc 定其行的位置, 再搜寻abc 行后面的字符串123,在其后面一行插入另一个字符串 ccc.
即:在 123 和 456 之间 插入字符串 ccc.
如果只是这样的话,下面这样就可以了:

先读出文件到字串,用replace函数替换,写回文件
dim buff() as byte
dim i as long
dim mfile as string
mfile="c:\123.txt"
i=filelen(mfile)
redim buff(i-1)
open mfile for binary as #1
get #1,,buff
close #1
dim s as string
s=strconv(buff,vbunicode)
dim s1 as string
dim s2 as string
s1="123"+vbcrlf+"456"
s2="123"+vbcrlf+"ccc"+vbcrlf+"456"
s=replace(s,s1,s2)
kill mfile
open mfile for binary as #1
put #1,,s
close #1

jary12581 2003-11-17
  • 打赏
  • 举报
回复
1、我并不是刻意用数组,split函数是将多行文本中的每一直行自动填充数组,所以文本中有多少行,数组中的元素就有多少个。程序的执行步骤应该实现从文件中读取文本,然后将文本放到text中,接下的所有操作都只是对text中文本进性操作。
2、如果你想对文本进行插入操作,只能更改text重的文本,然后再将文本写入到文件。至于怎样进行动态插入,取决于程序的具体实现功能。
sslijun 2003-11-17
  • 打赏
  • 举报
回复
我这个file.txt文件,在打开状态,一边判断刚才写进去的所有字符串,一边继续不断写入下一个字符串,并且 原来的判断条件abc 123 也在变 比如变为yub 456 等等,如果用数组 最后 可能用到100万个,您看如何是好?
jary12581(狼) 请指教?
jary12581 2003-11-17
  • 打赏
  • 举报
回复
'重新声明字符数组的长度
dim str() as string
str=sprit(text1.text,vb.crlf)
dim strlength as integer=ubound(str)
dim linecount as string
dim i as integer
for i=0 to ubound(str)
dim s as string
dim j as integer
dim k as integer
s=str(i)
j=instr(s,"abc")
if j<>0 then
msgbox"你找的文本abc存在!" & "在第" & cstr(i+1) & "行。" '确定abc的位置
for k=i+1 to ubound(str)
dim s1 as string
s1=str(k)
dim m as integer
m=instr(s1,"123")
if m<>0 then
msgbox"你找的文本123存在!" & "在第" & cstr(k+1) & "行。" '确定123的位置
redim preserve str(strlength+1) '重新声明字符数组的长度
str(i+1)="ccc"
end if
next
end if
text
sslijun 2003-11-17
  • 打赏
  • 举报
回复
'再次插入字符ccccc
'代码略
ccc 如何插入在 123 行后面,代码如何实现?
jary12581 2003-11-17
  • 打赏
  • 举报
回复
dim str() as string
str=sprit(text1.text,vb.crlf)
dim linecount as string
dim i as integer
for i=0 to ubound(str)
dim s as string
dim j as integer
dim k as integer
s=str(i)
j=instr(s,"abc")
if j<>0 then
msgbox"你找的文本abc存在!" & "在第" & cstr(i+1) & "行。" '确定abc的位置
for k=i+1 to ubound(str)
dim s1 as string
s1=str(k)
dim m as integer
m=instr(s1,"123")
if m<>0 then
msgbox"你找的文本123存在!" & "在第" & cstr(k+1) & "行。" '确定123的位置
……
'再次插入字符ccccc
'代码略
……
end if
next
end if
text



如又不明白之处,请在线交流。
cryptonym 2003-11-17
  • 打赏
  • 举报
回复
Function WriteLineToFile
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.WriteLine "Hello world!"
f.WriteLine "VBScript is fun!"
Set f = fso.OpenTextFile("c:\testfile.txt", ForReading)
WriteLineToFile = f.ReadAll
End Function
Function ReadLineTextFile
Const ForReading = 1, ForWriting = 2
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
MyFile.WriteLine "Hello world!"
MyFile.WriteLine "The quick brown fox"
MyFile.Close
Set MyFile = fso.OpenTextFile("c:\testfile.txt", ForReading)
ReadLineTextFile = MyFile.ReadLine ' Returns "Hello world!"
End Function

7,771

社区成员

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

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