Hi!

I want to read a text file word by word to string and display it in a text box. Given below is my piece of code. If I give the input string as "this is my cat", this piece of code only displays 'cat'. whatever input i give, program on compilation shows only last word. What's wrong in it. Please help.

DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
            
            if (result == DialogResult.OK) // Test result.
            {
                string file = openFileDialog1.FileName;
                try
                {
                    // display the input file in textbox1
                    string text1 = File.ReadAllText(file);
                    textBox1.Text = text1;

                    //read the text file again
                    string s = File.ReadAllText(file);
                    string[] words = s.Split(' ');
                    foreach (string word in words)
                    {
                        // show the resulting string in textbox2
                        textBox2.Text=word;
                    }

                 
                }

Try This...

StreamReader sr = new StreamReader(Environment.CurrentDirectory + "\\abc.txt");
            string line = null;
           do
            {
                line = sr.ReadLine();
                
                if ((line != null))
                {
                     MessageBox.show(line);                         //Read line
                    foreach (char cnt in line)
                    {
                      textBox1.AppendText(cnt.ToString());         //Read characters
                    }

                }
            } while (!(line == null));
            sr.Close();

Hope this will help you.

Edited 5 Years Ago by bhagawatshinde: n/a

To read a file and to get word by word (seperated one from another), then you can doit this way:

List<string> list  = new List<T>();
using(StreamReader sr = new StreamReader("filePath"))
{
    string line;
    while((line = sr.ReadLine()) != null)
    {
        string[] words 0 line.Split(' ');
        foreach(string word in words)
             list.Add(word);
    }
}

Now you have all the words in the leneric list<T>.
If you want to "combine" them, you can do it this way:

textBox1.Text = String.Join(" ", list.ToArray());

Edited 5 Years Ago by Mitja Bonca: n/a

Your foreach loop runs so fast you will be at the word "cat" before you even notice it. Try putting in some slowing down in your loop, if you want to see each word seperately.

You may also consider using Regex.Split() to split on word boundaries and filter out the punctuation. If you split on ' ' and you have "hi\r\nhello" then it will show up as one word because but its really on two different lines. I suppose if you use .ReadLine() for the input file it will already handle the new line scenario but perhaps if someone ended a sentence with a period and didn't have a space after the punctuation...

void button15_Click(object sender, EventArgs e)
    {
      const string s1 = "hi. how are you?\r\nGood thanks";
      string[] arr1 = s1.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
      string[] arr2 = System.Text.RegularExpressions.Regex.Split(s1, @"\b");
      Debug.WriteLine("Method1: " + string.Join(" | ", arr1).Replace("\r", @"\r").Replace("\n", @"\n"));
      Debug.WriteLine("Method2: " + string.Join(" | ", arr2).Replace("\r", @"\r").Replace("\n", @"\n"));
    }

Results in:

Method1: hi. | how | are | you?\r\nGood | thanks
Method2:  | hi | .  | how |   | are |   | you | ?\r\n | Good |   | thanks |
This article has been dead for over six months. Start a new discussion instead.