C #: What is an efficient way to parse a single-separator line via ReadLine () TextReader?

C #: What is an efficient way to parse a line with one separator for each ReadLine () TextReader?

My task is to load the list of proxies into ListView in two columns (proxy | port) read from a TXT file. How could I split each readline () into a proxy and port variable with a separator ":"?

This is what I still have

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

If not, is there a better way to do this?

+5
source share
5 answers

You can break them down as follows:

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

It is best to use small letters for variables in C #, since others are reserved for class names / names, etc.

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

?

var parts=
    Regex.Matches(input, @"(?<left>[^:]*):(?<right>.*)",RegexOptions.Multiline)
    .Cast<Match>()
    .Where(m=>m.Success)
    .Select(m => new
        {
            left = m.Groups["left"],
            right = m.Groups["right"]
        });

foreach(var part in parts)
{
    //part.left
    //part.right
}

, , Linqify ReadLine yielding?

static IEnumerable<string> Lines(string filename)
{
    using (var sr = new StreamReader(filename))
    {
        while (!sr.EndOfStream)
        {
            yield return sr.ReadLine();
        }
    }
}

:

var parts=Lines(filename)
.Select(
    line=>Regex.Match(input, @"(?<left>[^:]*):(?<right>.*)")
)
.Where(m=>m.Success)
.Select(m => new
    {
        left = m.Groups["left"],
        right = m.Groups["right"]
    });
foreach(var part in parts)
{
    //part.left
    //part.right
}
+2

, :

    int index = line.IndexOf(':');
    if (index < 0) throw new InvalidOperationException();
    Proxy = line.Substring(0, index);
    Port = int.Parse(line.Substring(index + 1));

/ , Split, . , , , . , , ( ) :

myListView.BeginUpdate();
try {
    // TODO: add all the items here
} finally {
    myListView.EndUpdate();
}
+1

, - .

var items = File.ReadAllText(FilePath)
    .Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
    .Select(line => line.Split(':'))
    .Select(pieces => new { 
        Proxy = pieces[0], 
        Port = int.Parse(pieces[1]) 
    });

If you know that at the end of the file you will not have an empty line at the end of the file, you can do this.

var items = File.ReadAllLines(FilePath)
    .Select(line => line.Split(':'))
    .Select(pieces => new { 
        Proxy = pieces[0], 
        Port = Convert.ToInt32(pieces[1]) 
    });
0
source

All Articles