Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere to search?
    primarykey
    data
    text
    <p>I am currently doing a small C# exercise that involves a text file. All the text file has are sentences for each new line in the text file. So far, I am able to read and store the text into a string array. What I need to do next, is to search for a particular term and then write out any sentences that contain the searched word/phrase. I just want to know if I should do it inside the while loop or somewhere else?</p> <pre><code>String filename = @"sentences.txt"; // File.OpenText allows us to read the contents of a file by establishing // a connection to a file stream associated with the file. StreamReader reader = File.OpenText(filename); if (reader == null) { // If we got here, we were unable to open the file. Console.WriteLine("reader is null"); return; } // We can now read data from the file using ReadLine. Console.WriteLine(); String line = reader.ReadLine(); while (line != null) { Console.Write("\n{0}", line); // We can use String.Split to separate a line of data into fields. String[] lineArray = line.Split(' '); String sentenceStarter = lineArray[0]; line = reader.ReadLine(); } Console.Write("\n\nEnter a term to search and display all sentences containing it: "); string searchTerm = Console.ReadLine(); String searchingLine = reader.ReadLine(); while (searchingLine != null) { String[] lineArray = line.Split(' '); String name = lineArray[0]; line = reader.ReadLine(); for (int i = 0; i &lt; lineArray.Length; i++) { if (searchTerm == lineArray[0] || searchTerm == lineArray[i]) { Console.Write("\n{0}", searchingLine.Contains(searchTerm)); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload