C#:通过TextReader的ReadLine()解析带有一个分隔符的字符串的有效方法是什么?

Mic*_*vin 5 c# listview split

C#:对于TextReader的每个ReadLine(),用一个分隔符解析字符串有什么有效的方法?

我的目标是将ListView的代理列表加载到从.txt文件读取的两列(代理|端口)中.我如何继续使用分隔符":"将每个readline()拆分为代理和端口变量?

这是我到目前为止所得到的,

    public void loadProxies(string FilePath)
    {
        string Proxy; // example/temporary place holders
        int Port; // updated at each readline() loop.

        using (TextReader textReader = new StreamReader(FilePath))
        {
            string Line;
            while ((Line = textReader.ReadLine()) != null)
            {
                // How would I go about directing which string to return whether
                // what's to the left of the delimiter : or to the right?
                //Proxy = Line.Split(':');
                //Port = Line.Split(':');

                // listview stuff done here (this part I'm familiar with already)
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果没有,是否有更有效的方法来做到这一点?

Phi*_*off 2

你可以这样分割它们:

        string line;
        string[] tokens;
        while ((Line = textReader.ReadLine()) != null)
        {
            tokens = line.Split(':');
            proxy = tokens[0];
            port = tokens[1];

            // listview stuff done here (this part I'm familiar with already)
        }
Run Code Online (Sandbox Code Playgroud)

最佳实践是在 C# 中对变量使用小写字母名称,因为其他名称是为类/命名空间名称等保留的。