Search code examples
c#.netstring-concatenation

c# concatenation troubles (new to c#)


Alright so I'm trying to concatenate two strings from a text file together and store them into an array. The problem is is that it seems that when the strings are being concatenated it places the second string to the beginning of the first.

Code:

//grabs the lines from the text file
private static string[] ReadFile()
    {
        string line = "";

        try
        {
            using (StreamReader sr = new StreamReader("Links.txt"))
            {
                line = sr.ReadToEnd();
            }
            // Breaks the lines into categories
            string[] categories = BreakContentIntoCategories(line);
            return categories;
        }
        catch (Exception e)
        {
            Console.WriteLine("This file could not be read:");
            Console.WriteLine(e.Message);
        }
        return null;
    }

    private static string[] BreakContentIntoCategories(string lines)
    {
        string[] categories = new string[3];
        int place = 0;

        string[] line = lines.Split('\n');
        for (int i = 0; i < line.Length; i++)
        {
            if (line[i][0] == '#')
            {
                string[] category = new string[2];
                category[0] = line[i].Substring(1);
                category[1] = line[i + 1];
                Console.WriteLine(category[0]);
                Console.WriteLine(category[1]);
                // Problem lies here
                Console.WriteLine(category[0] + category[1]);
                Console.WriteLine("-----------------");
                categories[place] = string.Join("",category);
                place++;
            }
        }

        return categories;
    }

So when I do the

Console.WriteLine(category[0] + category[1]);

It returns

BOTW urlThe Worst

When it should return

Best Of The WorstBOTW url

Text File:

#Plinkett
plinkett url

#Half In The Bag
HITB url

#Best Of The Worst
BOTW url

Any help is appreciated and anymore info needed just tell me and I will add it to the post.

This has been solved thanks to an anonymouse user, Jakob Olsen, and Francesca Aldrovandi thank you all!

Solution: I had completely forgot that when you read a file there will be characters such as \r and \n added at the end and I wasn't removing them when reading the lines. So I'm currently using Francesca Aldrovandi's advice and using the replace method to easily remove them.

Thank you all for your advice on my problem!


Solution

  • You are splitting on "\n", but the newlines 'character' is in fact "\r\n". This means that most of your lines will end with "\r" which means that your Console.WriteLine doesn't work.... You are not printing the actual contents of the variables.

    I have modified the code a bit, so it should do what you expect.

    private static string[] BreakContentIntoCategories(string lines)
        {
            string[] categories = new string[3];
            int place = 0;
    
            lines = lines.Replace("\r\n", "\n");
    
            string[] line = lines.Split('\n');
            for (int i = 0; i < line.Length; i++)
            {
                if (line[i].StartsWith("#"))
                {
                    string[] category = new string[2];
                    category[0] = line[i].Substring(1);
                    category[1] = line[i + 1];
                    Console.WriteLine(category[0]);
                    Console.WriteLine(category[1]);
                    // Problem lies here
                    Console.WriteLine(category[0] + category[1]);
                    Console.WriteLine("-----------------");
                    categories[place] = string.Join("", category);
                    place++;
                }
            }
    
            return categories;
        }