[英]Adding a newline into a string from text file
我有一个文本文件。
我需要在文本的每个新行之后添加一个换行符,并将每个新行都用“”或//包围。
我的输出应该是这样的:
//名称脱离点//
//描述自动测试用例,用于记录分离点和该点所需的力行特性。
// StartRecording ForceTravel //
// UserInteraction,请开始尝试将齿轮换到1档。
//捕获DisengagementPoint UserInput == 1 PressClutch 1 UserInput == 1 ///// // UserInteraction请将齿轮换到空档。//
// ReleaseClutch 100 ForceTravel == LimitReleased //
读取文本文件的方法:
if (!File.Exists(measurementPath))
{
string[] readText = File.ReadAllLines(measurementPath);
foreach (string s in readText)
{
script = s.Replace(" ", " // ");
char[] separator = new char[] { ' ' };
String[] fields = s.Split(separator);
您可以使用File.ReadLines
,LINQ + String.Format
和File.WriteAllLines
:
var newLines = File.ReadLines(measurementPath)
.Select(line => String.Format("//{0}//", line))
.ToList();
File.WriteAllLines(measurementPath, newLines);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.