Adrian Moldovan
Adrian Moldovan

Reputation: 37

Adding a newline into a string from text file

I have a text file.

Example

I need to add a newline after every new line from text and put every new line surrounded by "" or //.

My Output should be like this:

//Name Disengagement point//
//Description Automated test case to record disengagement point and force-travel characteristic needed for the point.//
//StartRecording ForceTravel//
//UserInteraction Please, start attempting to shift the gear to 1st gear.//
//Capture DisengagementPoint UserInput == 1 PressClutch 1 UserInput == 1// //UserInteraction Please, shift the gear to neutral.//
//ReleaseClutch 100 ForceTravel == LimitReleased//

The method for reading text file:

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);

Upvotes: 2

Views: 78

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460380

You can use File.ReadLines, LINQ + String.Format and File.WriteAllLines:

var newLines = File.ReadLines(measurementPath)
    .Select(line => String.Format("//{0}//", line))
    .ToList();
File.WriteAllLines(measurementPath, newLines);

Upvotes: 4

Related Questions