gpt4 book ai didi

c# - 如何查找和替换文件中的文本

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:01 24 4
gpt4 key购买 nike

到目前为止我的代码

StreamReader reading = File.OpenText("test.txt");
string str;
while ((str = reading.ReadLine())!=null)
{
if (str.Contains("some text"))
{
StreamWriter write = new StreamWriter("test.txt");
}
}

我知道如何找到文本,但我不知道如何用我自己的文本替换文件中的文本。

最佳答案

读取所有文件内容。使用 String.Replace 进行替换。将内容写回文件。

string text = File.ReadAllText("test.txt");
text = text.Replace("some text", "new value");
File.WriteAllText("test.txt", text);

关于c# - 如何查找和替换文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44026569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com