【发布时间】:2018-11-22 21:58:09
【问题描述】:
评论//到结尾,我不知道如何干净地结束子字符串:( 有没有一种更简单的方法可以到达子字符串的末尾,而不是自己算出数字?对于更复杂的字符串,这太难了
string word = Console.ReadLine();
string[] lines = File.ReadAllLines(file);
using (var far = File.CreateText(resultfile))
{
foreach (string line in lines)
{
StringBuilder NewL = new StringBuilder();
int ind = line.IndexOf(word);
if (ind >= 0)
{
if (ind == 0)
{
NewL.Append(line.Substring(ind+ word.Length +1, // go to end);
}else{
NewL.Append(line.Substring(0, ind - 1));
NewL.Append(line.Substring(ind + word.Length + 1, // go to end));}
far.WriteLine(NewL);
}
else
{
far.WriteLine(line);
}
}
我不知道stackoverflow想要什么更多细节,任何能回答这个问题的人都可以清楚地理解这个简单的代码。
【问题讨论】:
-
您是否只是想从文件加载的输入行中删除某个单词然后重写这些行?