【问题标题】:Csharp substring text and add it to listCsharp 子字符串文本并将其添加到列表中
【发布时间】:2015-10-27 04:09:15
【问题描述】:

我有 file.txt 像: 编辑:我没有写,但我猜这很重要 - 在 file.txt 中可能有其他行!

folder=c:\user;c:\test;c:\something;

我需要添加一个路径,例如一个列表项 (List<string> Folders)。 所以我的列表应该是这样的:

Folders[0] = c:\user
Folders[1] = c:\test

等等。 (没有文本“folder=”,它在 file.txt 中开始行,“;”表示路径结束)。

文件可以包含更多路径。 我做了这样的事情:

       using (FileStream fss = new FileStream(path, FileMode.Open))
        {
            using (StreamReader sr = new StreamReader(fss))
            {

                while (sr.EndOfStream == false)
                {
                    string line = sr.ReadLine();
                    if(line.StartsWith("folders"))
                    { 
                       int index = line.IndexOf("=");
                       int index1 = line.IndexOf(";");
                       string folder = line.Substring(index + 1, index1 - (index + 1));
                       Folders.Add(folder);

现在在列表文件夹中我有第一个路径,但现在呢?我不能继续 :(

【问题讨论】:

  • 为什么不呢?什么东西阻止你?您可以通过修剪folder= 并通过; 拆分来简化此操作。然后您可以使用InsertRange 将整个拆分数组转储到您的列表中。完成。
  • 类似于 line.Trim(char["folders="]); ?

标签: c# string file substring


【解决方案1】:
using(var sr = new StreamReader(path))
{
    var folders = sr.ReadToEnd()
    .Split(new char[]{';','\n','\r'}, StringSplitOptions.RemoveEmptyEntries)
    .Select(o => o.Replace("folder=",""))
    .ToArray();
    Folders.AddRange(folders);
}

【讨论】:

  • 但是现在如何将每个路径添加到 List 中?
  • 您假设文件中只有一行。如果有多行以“folder=”开头怎么办?如果还有其他不应该处理的行怎么办?...
  • 其实还有其他几行。抱歉,我编辑问题。
  • @Kafus 在您的示例中只有一行。不知道,其他的怎么样
  • 太棒了!这就是我需要的:) 我应该只将sr.ReadToEnd() 替换为我的变量line 并且一切正常。非常感谢
【解决方案2】:

您可以尝试以下代码,使用 File.ReadAllText

            string Filepath = "c:\abc.txt";
            string filecontent = File.ReadAllText(Filepath);
            string startingString = "=";
            var startIndex = filecontent.IndexOf(startingString);
            filecontent = filecontent.Substring(startIndex + 1, filecontent.Length - startIndex - 2);
            List<String> folders = filecontent.Split(';').ToList();

【讨论】:

    【解决方案3】:

    这是一个简单的例子:

        List<String> Folders = new List<string>();
    
        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"C:\Users\mikes\Documents\SomeFile.txt";
    
            string folderTag = "folder=";
            using (FileStream fss = new FileStream(path, FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fss))
                {
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        if (line.StartsWith(folderTag))
                        {
                            line = line.Substring(folderTag.Length); // remove the folderTag from the beginning
                            Folders.AddRange(line.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
                        }
                    }
                }
            }
    
            foreach(string folder in Folders)
            {
                Console.WriteLine(folder);
            }
        }
    

    如果您要逐行阅读,我会使用这种方法,并根据每行开头的内容执行其他操作。在这种情况下,您可以添加不同的 else if(...) 块:

                        if (line.StartsWith(folderTag))
                        {
                            line = line.Substring(folderTag.Length); // remove the folderTag from the beginning
                            Folders.AddRange(line.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
                        }
                        else if(line.StartsWith("parameters="))
                        {
                            // do something different with a line starting with "parameters="
                        }
                        else if (line.StartsWith("unicorns="))
                        {
                            // do something else different with a line starting with "unicorns="
                        }
    

    【讨论】:

    • 是的,我的代码中有类似的东西。我的意思是else if。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2021-06-09
    • 2013-09-24
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    相关资源
    最近更新 更多