Hi,
I am new to C# and would appreciate some coding tips on the following program. I created a csv file called nameSurname.csv. The first column in my file is the name and second surname. I would like to do the following:
- display the name and surname on Console (without commas)
-write a third column to my csv called nameSurname which concatenated name and surname.
How can I improve on my coding? Is there any standards that I do not adhere to?
Thanks in advance.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.IO;
class
Program
{
static void Main()
{
int count;
count = 0;
// itterate through lines in file to create a concatenate a new string
string[] nameSurnameLine;//concatenated string of name and surname
string[] fileLines = File.ReadAllLines(@"C:\Users\LVK\Desktop\NameSurname.csv");
nameSurnameLine =
new string[fileLines.Length];
foreach (string value in fileLines)
{
string newLine;
int pos;
int start = 0;
newLine =
' '.ToString();
System.Text.
StringBuilder nameSurname = new System.Text.StringBuilder();
for (pos = 0; pos < value.Length; pos++)
{
if (value[pos] == ',')
{
// create a new line to write to the file. This line will constist out of old values
//and the new concatenated string
Console.Write(value.Substring(start, pos));
Console.Write(" ");
newLine = value.Substring(start, pos);
nameSurname.Append(value.Substring(start, pos));
start = pos + 1;
}
if ((pos==value.Length-1)& (start !=0) )
{
//if you are at the end of the line, add the value column to the new line and lastly add the
//concatenated string
newLine +=
',';
Console.Write(value.Substring(start, pos-start+1));
newLine += value.Substring(start, pos - start + 1);
newLine +=
',';
nameSurname.Append(value.Substring(start, pos - start + 1));
newLine += nameSurname.ToString();
nameSurnameLine[count] = newLine;
}
}
Console.WriteLine("");
count++;
}
File.WriteAllLines(@"C:\Users\LVK\Desktop\NameSurname.csv", nameSurnameLine);
Console.ReadKey();
}
}