C# 替换一个文本中的指定字符

123.txt里有很多行文本,其中有一行文本为123456789,现在我要把这行文本替换为9527,请问C#怎么写代码,要简洁的... 123.txt里有很多行文本,其中有一行文本为123456789,现在我要把这行文本替换为9527,请问C#怎么写代码,要简洁的 展开
 我来答
绝世腹黑男
推荐于2017-09-11 · TA获得超过148个赞
知道小有建树答主
回答量:272
采纳率:0%
帮助的人:89.4万
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
string path = @"config.txt";
string con = "";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
con = sr.ReadToEnd();
con=con.Replace("123456789", "9527");
sr.Close();
fs.Close();
FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs2);
sw.WriteLine(con);
sw.Close();
fs2.Close();

}

}

}
591PPT
2011-10-25 · 超过10用户采纳过TA的回答
知道答主
回答量:46
采纳率:0%
帮助的人:23.3万
展开全部
string path = @"123.txt";//文件路径
string con = "";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
con = sr.ReadToEnd();//读取文件内的内容
con=con.Replace("123456789", "9527");//读取后替换
sr.Close();
fs.Close();
FileStream fs2 = new FileStream(path, FileMode.Open, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs2);
sw.WriteLine(con);//新内容写入文件
sw.Close();
fs2.Close();
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
188838820
2011-10-25 · 超过18用户采纳过TA的回答
知道答主
回答量:91
采纳率:0%
帮助的人:73万
展开全部
string 类中有一个 substring 和 indexof 这两个方法 你一看就好了 直接替换掉了就
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友cdf898107
2011-10-25
知道答主
回答量:14
采纳率:0%
帮助的人:4.6万
展开全部
string.Format
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式