I parse the text file. You need to set the condition: "if the line starts with a number" and so on. How to implement it

  • Have you tried anything yet? Show. - andreycha
  • Google did not give results. Now I will describe in more detail what I need. There are such lines: 1.1 Text Description of the first paragraph 1.2 Text, etc. It is necessary to select only 1.1 Text (the line starts with a number), and skip the description. Any ideas, because, I repeat, Google did not give results? - Vlad
  • And what do you mean by "condition for StreamReader "? Make a detailed description of the question. - VladD
  • In theory, you need to take something from the line, or characters - in the number of how many characters from the beginning of the line or from such a character count for such a score in a line, or part of a line to the splitter (split). There can be several separators per line, respectively, you can take split (""; 0), split (""; 1), split (""; 2) and so on ... depending on how the file is saved and what is in it ... - SergeyE
  • one
    @Vlad Well, at least write how they decided. - andreycha

2 answers 2

There is such a thing as String.StartWith(s1, s2) ; Where s1 string you are checking, and s2 is a substring whose presence is checked at the beginning

We have the following code:

 StreamReader sr = new StreamReader(@puth); string buffer; // буфер для считывания строк while(!sr.EndOfStream) { buffer = sr.ReadLine(); if (String.StartWith(buffer, i.ToString()) // где і - нужное число, преобразованое в строку { // выполняешь нужные действия } } sr.Close(); 

You can also create a method that will perform the desired action depending on the containing number at the beginning of the read line, for example:

 public static string DoSomeOperationWithString(string s, string subS) { string result; if(subS == "1.1") { // выполняешь действия, записывая результат в переменную result, потом возвращаешь ее } else if(subS == "1.2") { // выполняешь действия, записывая результат в переменную result, потом возвращаешь ее } return result; } 

You call the method in the body of the read cycle, instead of if , and the check is performed directly in the method. You can also create submethods that will be called from this method. I hope I checked your question.

In general, the answer to your question is here: https://msdn.microsoft.com/ru-ru/library/baketfxw(v=vs.110).aspx

PS: my first response to StackOverFlow :)

  • one
    And why at you sr not in using ? If an exception happens inside, the file will remain open. - VladD
  • And so in general with the initiative :) - VladD
  • The StartsWith method StartsWith not static. The given code is not combined. It would be good to fix it. - Alexander Petrov
  • Instead of String.StartWith (text, param) write someText.StartWith (param), where someText is the string to be checked and param is the test substring. And yes, I missed a little about using - Swoker

The answer is found! Implemented with Regex (regular expressions):

 string line = sr.ReadLine(); RegexOptions ignoreCase = RegexOptions.IgnorePatternWhitespace; Regex reg = new Regex(@"^\s+[0-9]", ignoreCase); Match m = reg.Match(line);