How can we effectively read only integers from a text file in C #

In C ++, we can define a custom locale that allows the stream object to ignore non-digital values ​​in a file and read only integers.

Can we do something like this? How can we effectively read only integers from a text file? C # stream object using locale? If so, can we define a custom locale that we can use with a stream object to ignore unwanted characters when reading a file?


Here is one C ++ example that effectively counts the frequency of words in a text file:

Elegant ways to count the frequency of words in a file

+4
source share
1 answer

My suggestion:

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 corresponding digit in a more elegant way than resetting it to the console;)

+4
source

Source: https://habr.com/ru/post/1338743/


All Articles