-3

i want to add all the numbers from notepad to the array, give me the solution . notepad file content see photos:

enter image description here

Q.thanh
  • 41
  • 1
  • 5
  • Does this answer your question? [Split big file where lines are separated by semicolon and split parts can contain semicolon inside quotes](https://stackoverflow.com/questions/18188107/split-big-file-where-lines-are-separated-by-semicolon-and-split-parts-can-contai) –  Jun 05 '21 at 17:57

1 Answers1

0

If what you are trying to do is parse every number in the text file of yours to a Int32 and have a int array.

You would want to use File.ReadAllLines, to read every line from the file, string.Split each line by ';' and then parse every string number to a Int32:

string[] lines = File.ReadAllLines("numbers.txt");

List<int> numbers = new List<int>();

for (int i = 0; i < lines.Length; i++)
{
    string line = lines[i];

    if (!string.IsNullOrEmpty(line))
    {
        string[] stringNumbers = line.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

        for (int j = 0; j < stringNumbers.Length; j++)
        {
            if (!int.TryParse(stringNumbers[j], out int num))
            {
                 throw new OperationCanceledException(stringNumbers[j] + " was not a number.");
            }
            numbers.Add(num);
        }
    }
}
int[] array = numbers.ToArray();

If you are looking to have a string array of the numbers then just do as the above, but without the int.TryParse and have numbers be a List<string> instead of List<int>

Space
  • 175
  • 9