Csharp子串文本并将其添加到列表中

时间:2015-08-04 16:31:04

标签: c# string file substring

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

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

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

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

等。 (没有文字&#34;文件夹=&#34;它在file.txt和&#34;;&#34;这意味着路径的结束)开始行。

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

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

现在在列表文件夹中我有第一条路径,但现在呢?我无法继续:(

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

您可以使用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();

答案 2 :(得分:0)

这是一个简单的例子:

    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="
                    }