Subscribe to danzar's Blog        RSS Feed
-----

Mega Project ideal list. - "Text" section / Count Words in a String

Icon Leave Comment
I take the text from a text box and test if its empty. If its not, then I split the string into and array by splitting it at the open space. Then I loop all the array items counting them. Then pass the results to a message box.

Second part is it opens a word file. Looping though it counting the words and then displays the results in a message box.




 private void countstringCount_bt_Click(object sender, RoutedEventArgs e)
        {
            if (countwordInput_tb.Text != "")
            {
                int counter = 0;
                string[] split_words = countwordInput_tb.Text.Split(' ');
                foreach (string word in split_words)
                {counter++;}
                MessageBox.Show("Word count =" + counter);
               
            }
        }

        private void countwordFileCount_bt_Click(object sender, RoutedEventArgs e)
        {
            StreamReader streamReader = new StreamReader(@"C:\wordlist.txt");
            string text = streamReader.ReadToEnd();
            streamReader.Close();
            int counter = 0;
            char[] delimiter = new char[] {'\r','\n',' '};
            string[] split_words = text.Split(delimiter);
            foreach (string word in split_words)
            { counter++; }
            MessageBox.Show("Word count =" + counter);
        }

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

December 2014

S M T W T F S
 123456
78910111213
1415161718 19 20
21222324252627
28293031   

Tags

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)