c# 连接问题(c# 新手)

c# concatenation troubles (new to c#)

好的,所以我正在尝试将文本文件中的两个字符串连接在一起并将它们存储到一个数组中。问题是,似乎在连接字符串时,它会将第二个字符串放在第一个字符串的开头。

代码:

//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;
    }

所以当我做

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

它returns

BOTW urlThe Worst

什么时候应该 return

Best Of The WorstBOTW url

文本文件:

#Plinkett
plinkett url

#Half In The Bag
HITB url

#Best Of The Worst
BOTW url

感谢任何帮助,如果需要任何信息,请告诉我,我会将其添加到 post。

感谢匿名用户 Jakob Olsen 和 Francesca Aldrovandi,此问题已得到解决,谢谢大家!

解决方法: 我完全忘记了当你读一个文件时会有像 \r 和 \n 这样的字符加在最后,我没有删除它们阅读台词。所以我目前正在使用 Francesca Aldrovandi 的建议并使用替换方法轻松删除它们。

谢谢大家对我的问题提出的建议!

您在“\n”上拆分,但换行符 'character' 实际上是“\r\n”。这意味着你的大部分行将以“\r”结尾,这意味着你的 Console.WriteLine 不起作用......你没有打印变量的实际内容。

我稍微修改了代码,所以它应该符合您的预期。

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;
    }

我认为您的问题出在您阅读文本文件的方式上。 StreamReader.ReadToEnd 方法将在您的字符串中添加 \n、\r 字符。 让我们在这里关注 \r 字符,它是回车 return。 当您连接包含该字符的不同字符串时,您将丢失第一个字符串的一部分,第二个字符串将首先打印。 尝试从字符串中删除 \r 字符

您需要删除行尾的“\r”字符。

category[0] = line[i].Substring(1).Replace("\r", "");
category[1] = line[i + 1].Replace("\r", "");