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/