4

In C++, we can define a custom locale that enables stream object to ignore non-digits in the file, and reads only the integers.

Can we do something similar? How can we efficiently read only integers from a text file? Does C# stream object use locale? If yes, can we define custom locale that we can use with stream object so as to ignore unwanted characters while reading the file?


Here is one example in C++ which efficiently counts frequency of words in a text file:

Elegant ways to count the frequency of words in a file

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851

1 Answers1

4

My proposal:

            public void ReadJustNumbers()
            {
                Regex r = new Regex(@"\d+"); 
                using (var sr = new StreamReader("xxx"))
                {
                    string line;
                    while (null != (line=sr.ReadLine()))
                    {
                        foreach (Match m in r.Matches(line))
                        {
                            Console.WriteLine(m.Value);
                        }
                    }
                }
            }

where xxx is the file name, obviously you will use the matching digit in a more elegant way than dumping on the console ;)

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115