如何替换文本文件中的文本字符串 [英] How to replace text string in a text file

查看:95
本文介绍了如何替换文本文件中的文本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我是vb.net的新手,我正在尝试做一些非常简单的事情。我有这个代码从.ini文件读取某些文本行。



Hello,

I am new to vb.net and am trying to do something really simple. I have this code which reads certain line of text from .ini file.

Dim FilePath As String

FilePath = Application.StartupPath & "\bin\userconfig.ini"
IO.File.WriteAllText(FilePath, IO.File.ReadAllText(FilePath).Replace("UserName = ", TextBox_NewUser.Text)





如何使用TextBox_NewUser中键入的内容替换=之后的那行文本。正如您在当前代码中看到的那样它只是替换了我不想要的整个UserName =。



默认情况下.ini中的特定文本行具有此值:

UserName =未命名



那么如何使用TextBox_NewUser中键入的内容替换未命名?



我们非常感谢您的帮助。



我的尝试:



我没有尝试过多,因为我是VS中的菜鸟。



How do I make it replace that line of text after the "=" with something you type in TextBox_NewUser. As you can see with current code it just replaces the whole "UserName =" which I don't want.

That specific line of text in the .ini by default has this value:
"UserName = Unnamed"

So how do I make it replace just that "Unnamed" with something I type in TextBox_NewUser?

Any assistance will be most appreciated.

What I have tried:

I haven't tried much since I'm a noob in VS.

推荐答案

应该很明显它替换UserName =部分,因为这正是您要求它做的。

为什么不替换未命名部分呢?类似于:

It should be obvious that it replaces the "UserName = " part because this is exactly what you are asking it to do.
Why not replacing the "Unnamed" part instead? Something like:
IO.File.WriteAllText(FilePath, IO.File.ReadAllText(FilePath).Replace("Unnamed", TextBox_NewUser.Text)


这很简单,见下文摘要

This is something really easy, see below snippet
'import System.IO
dim text as string = File.ReadAllText("test.txt")
text = text.Replace("some text", "new value")
File.WriteAllText("test.txt", text)


这篇关于如何替换文本文件中的文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆