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

标签 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)
            }
        }
    }

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

最佳答案

string [] parts = line.Split(':');
string proxy = parts[0];
string port = parts[1];

关于C#:通过 TextReader 的 ReadLine() 解析带有一个定界符的字符串的有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2383603/

相关文章:

c# - 为什么 { } 初始化需要 Add 方法?

java - 为什么C#对 'Constructor call must be the first statement in a constructor'没有限制

python - 如何根据列中值的差异拆分 Pandas 数据框

java - 分成单独的数组

java - 用双引号分割字符串-Java

c# - 为什么要使用 ChannelFactory 来实例化 WCF 代理而不是服务引用?

c# - 使用具有代码访问安全性的 C# 迭代器方法时出现问题

jquery - ListView 过滤器不过滤新项目

android - 打开相机后 Activity 变为横向

java - 无法将数组发送到android中的listview