C # removing last delimited field from split string

I am a beginner C # programmer and just asked a question about an application I am building. My process reads across multiple files for the purpose of deleting specific entries based on a 1 or 0 delimited field in a text file. This is actually the last field in the file. If it is 0, I write it in a temp file (which will later replace the original I read), if it is something else that I am not doing. And not trying to make it too confusing, but there are two types of entries in the file, a header line and then followed by multiple supp lines. The header line is the only one that has a flag so you can tell from the bottom if bool gets a good entry being 0 it writes the header entry along with all supp entries below until it hits a bad one in which case it will deny their record until the next good.

However, what I am trying to do now (and would like to know the easiest way) is how to write the header record without the last channel-limited field (IE flag). Since it must always be the last 2 characters of the string (eg "0 |" or "1 |" since the previous pipe is needed), should it be a string trim in my I / O line? Is there an easier way? Is there a way to do the split by record, but not actually include the last field (in this case, field 36)? Any advice will be taken into account. Thank,

static void Main(string[] args)
{
    try
    {
        string executionDirectory = RemoveFlaggedRecords.Properties.Settings.Default.executionDirectory;
        string workDirectory = RemoveFlaggedRecords.Properties.Settings.Default.workingDirectory;

        string[] files = Directory.GetFiles(executionDirectory, "FilePrefix*");             
        foreach (string file in files)
        {
            string tempFile = Path.Combine(workDirectory,Path.GetFileName(file));
            using (StreamReader sr = new StreamReader(file,Encoding.Default))
            {
                StreamWriter sw = new StreamWriter(tempFile);
                string inputRecord = sr.ReadLine();

                bool goodRecord = false;
                bool isheaderRecord = false;
                while (inputRecord != null)
                {
                    string[] fields = inputRecord.Split('|');
                    if (fields[0].ToString().ToUpper() == "HEADER")
                    {                               
                        goodRecord = Convert.ToInt32(fields[36]) == 0;
                        isheaderRecord = true;
                    }

                    if (goodRecord == true && isheaderRecord == true)
                    {       
                        //    I'm not sure what to do here to write the string without the 36th field***
                    }
                    else if (goodRecord == true)
                    {
                        sw.WriteLine(inputRecord);
                    }

                    inputRecord = sr.ReadLine();
                }

                sr.Close();
                sw.Close();
                sw = null;
            }    
        }

        string[] newFiles = Directory.GetFiles(workDirectory, "fileprefix*");
        foreach (string file in newFiles)
        {
            string tempFile = Path.Combine(workDirectory, Path.GetFileName(file));
            string destFile = Path.Combine(executionDirectory, Path.GetFileName(file));

            File.Copy(tempFile, destFile, true);
            if (File.Exists(destFile))
            {
                File.Delete(tempFile);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }

    finally
    {
        // not done   
    }
}

      

+3


source to share


2 answers


One way to do this - if what you want at this point in your code is to always write everything but the end item in string[]

-, build a loop for

that ends before the last item:

for (int i = 0; i < fields.Length - 1; i++)
{
     // write your field here
}

      

It assumes that you want to write each field separately, and that you want to iterate through fields

in the first place. If all you want to do is just write one line to one line without using a loop, you can do this:



var truncatedFields = fields.Take(fields.Length - 1);

      

And then just write truncatedFields

string[]

as you see fit. One way that you could accomplish all of this in one line might look like this:

sw.WriteLine(String.Join("|", fields.Take(fields.Length - 1)));

      

+3


source


goodRecord = fields.Last (). Trim () == "0";



if (inputRecord.Contains ("|") string outputRecord = inputRecord.Substring (1, inputRecord.LastIndexOf ("|"));

0


source







All Articles