Home > Mobile >  Why is my Byte[] array combining these two strings?
Why is my Byte[] array combining these two strings?

Time:11-21

I'm coming back to C# after quite a while away from it, but this one is confusing me.

I have a .txt file (strRunnerTXTFile) on my website, and its contents are being read into a variable.

The text file (being read into strRunnerTXTFile) contains this:

"""BrandName"""
No
"""Brand\\Runner 1 Front"""
150mm
"""Brand\\Runner 2 Front"""
198mm
"""Brand\\Runner 3 Front"""
230mm
"""Brand\\Runner 4 Front"""

After it is read into the variable, the above code now looks like this:

"""BrandName"""
No
"""Brand\\Runner 1 Front"""
150mm
"""Brand\\Runner 2 Front"""198mm
"""Brand\\Runner 3 Front"""
230mm
"""Brand\\Runner 4 Front"""

The code I'm using to read the file into the variable is this:

WebClient wc = new WebClient();  // create object to use to access web data
byte[] raw = wc.DownloadData(strRunnerTXTFile);  // read data text file into variable
if (raw.Length == 0) { ExitWithError("Could not source data from server, or data file is empty.", 5); }
string webData = System.Text.Encoding.UTF8.GetString(raw);  // convert into usable format

string[] strTXTInput = webData.Split('\n');   // split array into indexes by new line separation
sRunnerSetName = strTXTInput[0].Replace("\"","");
    for (x = 0; x < strTXTInput.Length-1; x  )
    {
        switch (x)
        {
            case 0:
                sRunnerSetName = strTXTInput[x];
                break;
            case 1:
                sFH = strTXTInput[x];
                break;
            case 2:
                sR1 = strTXTInput[x];
                break;
            case 3:
                sH2 = strTXTInput[x];
                break;
            case 4:
                sR2 = strTXTInput[x];
                break;
            case 5:
                sH3 = strTXTInput[x];
                break;
            case 6:
                sR3 = strTXTInput[x];
                break;
            case 7:
                sH4 = strTXTInput[x];
                break;
            case 8:
                sR4 = strTXTInput[x];
                break;
            case 9:
                sH5 = strTXTInput[x];
                break;
            case 10:
                sR5 = strTXTInput[x];
                break;
            default:
                break;
        }
    }

createOutputString(RunnerSetFile);

And then later on ...

public static void createOutputString(string RunnerSetFile)
    {
        List<Item> list = new List<Item>
        {
            new Item { Description = sRunnerSetName, SortOrder = iRunnerSetName },
            new Item { Description = sFH, SortOrder = iFH },
            new Item { Description = sR1, SortOrder = iR1 },
            new Item { Description = sH2, SortOrder = iH2 },
            new Item { Description = sR2, SortOrder = iR2 },
            new Item { Description = sH3, SortOrder = iH3 },
            new Item { Description = sR3, SortOrder = iR3 },
            new Item { Description = sH4, SortOrder = iH4 },
            new Item { Description = sR4, SortOrder = iR4 },
            new Item { Description = sH5, SortOrder = iH5 },
            new Item { Description = sR5, SortOrder = iR5 }
        };

        list = list.OrderBy(x => x.SortOrder).ToList();
    }

It seems to be something in the final line there, where it sorts the order. But for the life of me, I cannot figure out why it's combining the two lines. Hopefully one of you can figure this out for me?

CodePudding user response:

quick solution: Look for \n after each two """, Add one if you don't find one.

CodePudding user response:

It is better not to use the switch case structure by index for parsing this format. In a good way, it is better to replace 3 quotes with some unique character that is guaranteed not to be present in the text. In the example below, we accumulate characters until we encounter a key character.

public sealed class BrandInfo
{
    public string Name { get; set; }

    public string About { get; set; }

    public override string ToString()
    {
        return $"{Name};{About}";
    }
}

internal static class BrandHelper
{
    public static void Parse(string webData)
    {
        var data = webData.Replace("\"\"\"", "\"");
        var startBrand = false;
        BrandInfo brand = null;
        var brandList = new List<BrandInfo>();
        var sb = new StringBuilder(100);
        for (int i = 0; i < data.Length; i  )
        {
            var c = data[i];
            switch (c)
            {
                case '"':
                    if (startBrand)
                    {
                        startBrand = false;
                        if (brand != null)
                        {
                            brand.Name = sb.ToString();
                            sb.Clear();
                        }
                    }
                    else
                    {
                        if (brand != null)
                        {
                            brand.About = sb.ToString();
                            sb.Clear();
                            brandList.Add(brand);
                        }

                        startBrand = true;
                        brand = new BrandInfo();
                    }
                    break;
                case '\n':
                case '\r':
                    break;
                default:

                    sb.Append(c);
                    break;
            }
        }

        var orders = brandList.OrderBy(x => x.About);
        foreach (var item in orders)
        {
            Console.WriteLine(item);
        }
        
    }
}

Result will be

  • Related