Good day everyone. Such a problem, you need to find a specific word in the text file, for example "name" and write the following word into the variable after it is found. For example: Petya's name found a name, Petya wrote in a variable . It is clear that you need to navigate the separation of words by spaces, but I don’t know how to implement it. In novice programming.

  • And what have you tried? - Batanichek

4 answers 4

  1. Read the data from the test file.
  2. Split resulting part using the Split function, using the space as a separator.
  3. Find the desired value in the resulting array and get its index.
  4. From the array, take the value with the index 1 or 2 more from that obtained in the previous step.
  5. Use it.

    When using such a code, the next word will be selected, but then there is a problem with the choice of a surname, for example)

     var inputStr = "some text name Petya Petyochkin some text some text"; var nameValue = "name"; var inputStrAList = inputStr.Split(' ').ToList(); string result = inputStrAList[(inputStrAList.IndexOf(nameValue) + 1)]; //result = "Petya" 
    • 3
      But this construction will replace all occurrences of the word “name” with “Peter”. It seemed to me that the author of the question needs something else. - Aleksandr Zharinov
    • @AleksandrZharinov thanks. Realized the error and corrected. The author does not have an exact description of the problem, he would have to edit - Yurii Manziuk
    • @Yurii Manziuk Thanks for the reply. But isn't the problem of finding the last name resolved by concatenating with inputStrAList [(inputStrAList.IndexOf (nameValue) + 3)] ;? - Nikita Bliznyuk
    • @NikitaBliznyuk there is an ambiguous situation and come into force IF: there is no name, and in the "+3" index some word. And at this very moment you are using what you found (name and surname) in another place. It will not be nice when the name “Petya blablabla” pops up IF: the surname is given, but you simply do not read it (do not use the “+3” index). It will not be beautiful when the name "Peter" pops up, although it says "Peter Peter Petechkin" here you need to look towards the correct format of the input text from the file - Yurii Manziuk
    • @Yurii Manziuk input files have a fairly strict structure, so I don’t think this will be a problem. Nevertheless, thank you again for your comments. - Nikita Bliznyuk

    Thanks to everyone for the answers, he took a bit of everything from everywhere:

     private void SearchInTxt(string fileLocation, string keyWord) { string str = File.ReadAllText(fileLocation); var kek = str.Split(new Char[] { ' ', '.', ',', ':', '\t' }).ToList(); foreach (string s in kek) { if (s.Trim() != "" && Regex.IsMatch(s, keyWord)) { textBox1.Text = kek[kek.IndexOf(keyWord) + 1] + " " + kek[kek.IndexOf(keyWord) + 3]; break; } } } 

      Try this:

       // Чтение из файла string dataFromFile = System.IO.File.ReadAllText("<путь к файлу>"); // Парсинг строки в массив с указанным разделителем string[] words = dataFromFile.Split(' '); 

      Next, look for the desired element in the array and take the next one.