VB 实现txt字符替换

首先存在一个排好版的txt文件。
用vb实现,将txt里所有的"abc"字符串,替换为"xyz",并保存。
谢谢!

Dim tmpStr As String, resStr As String, a As String

'读取txt
Open "d:\vb.txt" For Input As #1
Do While Not (EOF(1))
Input #1, a
tmpStr = tmpStr & a
Loop
Close #1

'开始处理字符串
resStr = Replace(tmpStr, "abc", "xyz")

'保存
Open "d:\vb_replace.txt" For Output As #1
Print #1, resStr
Close #1
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-12-01
下面是VBA的,你可以试试在vba里面替换,可能更简单,VB 下先打开文本文件,逐行读取文本,判断是否包含“abc”,发现是“abc”的替换为“xyz”后重新组合作为新数组保存,最后保存。

Sub Macro1()
'
' Macro1 Macro
'

'
Cells.Find(What:="abc", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, MatchByte:=False, SearchFormat:=False).Activate
ActiveCell.Replace What:="abc", Replacement:="xyz", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.FindNext(After:=ActiveCell).Activate
End Sub
第2个回答  2010-12-01
Private Sub Command1_Click()
Open "E:\临时文件\1.txt" For Input As #1
Line Input #1, Line_text
m = Line_text
Close #1
m = Replace(m, "ABC", "XYZ")
Open "E:\临时文件\1.txt" For Output As #1
Print #1, m
Close #1
End Sub

vb TXT文件内容替换
s = Replace(s, Chr(34), "")Text1.Text = s 希望你有用

VB 实现txt字符替换
'读取txt Open "d:\\vb.txt" For Input As #1 Do While Not (EOF(1))Input #1, a tmpStr = tmpStr & a Loop Close #1 '开始处理字符串 resStr = Replace(tmpStr, "abc", "xyz")'保存 Open "d:\\vb_replace.txt" For Output As #1 Print #1, resStr Close #1 ...

vb怎么样找到TXT文档里的物理地址然后用字符串替换掉
好了,建一个Command1.这个程序会把所有格式为"xx-xx-xx-xx-xx-xx"的字符替换掉。你可以尝试在命令提示符里面,输入ipconfig \/all >d:\\a.txt,然后运行我的程序来实验。代码如下。=== Private Sub Command1_Click()Dim a() As String, i As Integer, p As Integer Open "d:\\a.txt" ...

如何用VB编写替换.txt文件里的指定字符啊?
ffff = combo1.text open "路径.txt" for input as #1 '读出 do while not eof(1)line input #1 ,k s = s & k & vbcrlf loop close #1 s = replace(s,"dddd",ffff)open "路径.txt" for output as #1 '写入 print #1 , s close #1 ...

VB 替换TXT的某一行
用临时文件做,其中需要替换的txt为"C:\\1.txt"这个你可以自己改!Private Sub Command1_Click()' 单击command1 Dim FileRead As Integer '定义 Dim FileWrite As Integer Dim Str As String FileRead = FreeFile ‘调用FreeFile函数·获取可用文件号 Open "C:\\1.txt" For Input As FileRead ...

VB怎么改写TXT中某段字符
' LineNumber = 要替换的行数(数字) ' LineText ='要替换的字符文字 '返回: True 为正确执行, false 其它失败错误 '需要: 引用-- Microsoft Scripting Runtime 'Example: 把C目录下的MYFILE.TXT的第三行的文本替换为 Abcde ' ReplaceLine("C:\\Myfile.txt", 3,"Abcde") '作者MSDN中的ewang11 '___...

用VB实现文本查找和替换
IntKs = Txtpaper.SelStart + Len(StrCz)Txtpaper.SelLength = Len(StrCz)I = I + 1 lblFinded = "已经找到" & I & "处"K = MsgBox("找到第" & I & "处“" & StrCz & "”,是否替换?", vbYesNo, "找到第" & I & "处“" & StrCz & "”")If K = 6 Then Clipbo...

VB 批量字符替换—替换少量词的话我可以用replace,多个数据怎么办呢...
替换为的 这里面要一一对应 代码 Dim n Dim a() As String Private Sub Command1_Click()Open "d:\\1.txt" For Input As #1 Do Until EOF(1)n = n + 1 ReDim Preserve a(n) As String Line Input #1, a(n)Loop Close For i = 1 To n Step 2 Call tihuan(a(i), a(i + ...

vb替换txt文本内容,要求换后不换行,替换的内容可以后期自已设定_百度...
不写代码了,给个思路吧:1、读取 1.txt 文件的内容,存放到字符变量 s 中 2、替换 s 中的内容,字符"abc"为需要替换掉的内容,需要替换成的内容为text1.text :s=replace(s,"abc",text1.text)3、将处理后的字符重新另存为一个文件 ...

如何用vb更改txt文件中某一行的内容
Dim strnow = Format(Now(), "yyyymmddhh")TextBox1.Text = Replace(TextBox1.Text , strnow , strnow & " " & "你的内容")改了一下,原理就是用replace重写

相似回答