VB 文本指定行字符串的替代

来源:百度知道 编辑:UC知道 时间:2024/05/16 10:05:28
请教 如何利用VB进行文本字符串的替换 如文本位置在d盘根目录下,名为1.txt,原文本内容为
1 2 3
4 5 6
7 8 9
需要把第二行的第2个字符串5替换为12,把第三行的第1个字符串7替换为10,替换后的文本名称不变,内容更新为:
1 2 3
4 12 6
10 8 9
谢谢各位大侠!!
请注意是对指定的行列位置(或数组)进行操作,而不是先搜具体的数值如5和7再更新,谢谢!!!!遇到更棘手的问题了,文本的内容比原来的更复杂,不规范了。源文件内容变为a c d 1 2 3;e f 4 5 6;g 7 8 9。需要变为a c d 1 2 3;e f 4 12 6;g 10 8 9。要求跟原问题要求一致,谢谢各位大虾

'用VBS写的,做个参考吧!

dim m(3,3) '要根据内容所定
set fso=createobject("scripting.filesystemobject")
set file=fso.opentextfile("1.txt")
while file.atendofstream<>true
h=h+1
n=file.readline:s=split(n):l=ubound(s)
for i=0 to l
m(h,i+1)=s(i)
next
wend
file.close

m(2,2)="12":m(3,1)="10" '可以根据你想替换的内容进行修改

for i=1 to 3
for j=1 to 3
if j=3 then
sm=sm & m(i,j) & vbcrlf
else
sm=sm & m(i,j) & " "
end if
next
next

set file=fso.opentextfile("1.txt",2)
file.write sm
file.close

createobject("wscript.shell").run "notepad.exe 1.txt"