-1

I have a .txt file with this example, with a lot of lines:

20200610;My name ;00000001;Name of my company for example; this is the end;01;

I need to transform this layout to a positional one. the first column with 10 chars, the second one with 30, etc. after this, generate a new file with this new layout.

20200610  My name                       00000001(etc)

How can I do this transformation in C#? the file is already read.

  • Are you encountering a problem when you try to do that? If so, we need to see your code and understand more about the issue you're facing. – devlin carnate Jun 19 '20 at 17:53
  • Actually, I'm stuck in this logic, because I can't find a way to separate by " ; " and append these blank spaces – ianpolitavares Jun 19 '20 at 18:01
  • string replace or character inspection? and the number of spaces is dependent on a counter that tracks how many `;` have previously been encountered. – devlin carnate Jun 19 '20 at 18:05
  • 1
    Use [String.Split](https://learn.microsoft.com/en-us/dotnet/api/system.string.split) method. – Alexander Petrov Jun 19 '20 at 18:13
  • the information is the same, but in the case of the Date, I have 8 chars and a " ; ", I need to see how many chars are missing to complete with spaces and delete the semicolon: `20200610;` = 8 chars + ; `20200610 ` = 10 chars without ' ; ' – ianpolitavares Jun 19 '20 at 18:13
  • so, split by the delimiter and check the length of each resulting string and pad it however you need to. – devlin carnate Jun 19 '20 at 18:19
  • 1
    There are a lot of gotcha's when parsing a comma-delimited file. It is better to use a library specifically intended for it, e.g. [see this answer](https://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-with-header). Once you have read the file you can output it again with the required spacing by using `string.Format` (see [this answer](https://stackoverflow.com/a/644171/2791540)). – John Wu Jun 19 '20 at 19:45

2 Answers2

1

Assuming a very simple (no quoted fields) input, use String.Split then use String.Format with the alignment component, interpolating in the field lengths.

For example:

var src = "20200610;My name ;00000001;Name of my company for example; this is the end;01;";

var fieldLens = new[] { 10, 30, 8, 30, 30, 2, 1 };

var res = src.Split(';').Select((s, n) => String.Format($"{{0,{-fieldLens[n]}}}", s)).Join();
NetMage
  • 26,163
  • 3
  • 34
  • 55
0

A possible solution by creating a string extension method

// StringExtension.cs
public static class StringExtension
{
    public static String ToCustomLength(this String value, int cnt)
    {
        if (value.Length == cnt)
            return value;
        else if (value.Length < cnt)
            value = value.PadRight(cnt, ' ');
        else
            value = value.Substring(0, cnt);
        return value;
    }
}


// Program.cs
class cData
{
    public string SomeDate { get; set; }
    public string SomeName { get; set; }
    public string SomeID { get; set; }
    public string SomeCompany { get; set; }
    public string SomeCompany2 { get; set; }
    public string SomeID2 { get; set; }
    public cData(string t)
    {
        // Enforce the rules here
        string[] t2 = t.Split(';');
        if (t2.Count() != 7)
            throw new Exception("Invalid String passed");

        SomeDate = t2[0].ToCustomLength(10);
        SomeName = t2[1].ToCustomLength(30);
        SomeID = t2[2].ToCustomLength(20);
        SomeCompany = t2[3].ToCustomLength(30);
        SomeCompany2 = t2[4].ToCustomLength(30);
        SomeID2 = t2[5].ToCustomLength(5);
    }
}

class Program
{
    static void Main(string[] args)
    {
        cData data = new cData("20200610; My name; 00000001; Name of my company for example; this is the end; 01;");
    }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501