C# - Create text converter tool - getsolutionhub.

In this article we will see how to create Text Converter Tool that can be used to count words, characters, convert text to UPPERCASE or lowercase etc.

For that we will use some Methods, Constructor Etc..

  1.    ToLower() : Returns a copy of this string converted to lowercase.
  2.    ToUpper() : Returns a copy of this string converted to uppercase.
  3.    Clear() : Clears all text from the text box control.
  4.    ToCharArray() : Converts our string object to character array.
  5.    Clipboard class : Provides methods to place data on and retrieve data from the system clipboard.
  6.    SetText(string text) Method : Clears the clipboard and then adds text data in the TextDataFormat. Text or TextDataFormat. UnicodeText format, depending on the operating system.
  7.    GetText() Method : Retrieves text data from the clipboard in the TextDataFormat. Text or TextDataFormat. UnicodeText format , depending on the operating system.
  8.    Replace(string oldValue , string newValue) Method : Returns a new string in which all occurrences of a specified string in the current instance are replaces with another specified string.
  9.    Regex class : Parsing the inputing text for the regular expression pattern. Identify the regular expression pattern in the given text.
  10.    Matches() : Searches the specified input string for all occuurences of a regular expression. Return : A collection of the Match objects found by the search. If no matches are found, the method returns an empty collection object.
  11.    Match object in foreach loop : Represents the results from a single regular expression match.

Example :

using System.Windows.Forms;
using System.Text.RegularExpressions;
using System;

namespace getsolutionhubDemo
{
    public partial class textCoverter : Form
    {
        public textCoverter()
        {
            InitializeComponent();
        }
        public void wordCharSpacesCounter()
        {
            //Character counting start here
            int charCount = mainTxtBox.Text.Length;

            //Words counting start here
            int wordCount = 0;
            Regex regex = new Regex("\\s+");
            foreach (Match match in regex.Matches(mainTxtBox.Text))
            {
                wordCount++;
                if (mainTxtBox.Text.Length < 1)
                {
                    wordCount = 0;
                }
            }

            //Spaces counting start here
            int numSpace = 0;
            foreach (char c in mainTxtBox.Text.ToCharArray())
            {
                if (c == ' ')
                {
                    numSpace++;
                }
            }
            wordLetterCounter.Text = "Words : " + wordCount + " , Characters : " + charCount + " , Spaces : " + numSpace;
        }
        private void mainTxtBox_KeyUp(object sender, KeyEventArgs e)
        {
            previewTxtBox.Text = mainTxtBox.Text;
            //Words: 0 , Characters: 0 , Spaces: 0

            wordCharSpacesCounter();
        }
        //ClearTextBoxes method start here
        public void clearTextBoxes()
        {
            mainTxtBox.Clear();
            previewTxtBox.Clear();
            wordLetterCounter.Text = "Words : 0 , Characters : 0 , Spaces : 0";
        }
        private void clearBtn_Click(object sender, System.EventArgs e)
        {
            //Creating clearTextBoxes() method for clear box textboxes text
            clearTextBoxes();
        }
        //UPPERCASE button start here
        private void uppercaseBtn_Click(object sender, System.EventArgs e)
        {
            if (mainTxtBox.Text != "")
            {
                string str = mainTxtBox.Text;
                previewTxtBox.Text = str.ToUpper();
            }
            else
            {
                MessageBox.Show("Please Enter text in the textbox.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
        }
        //lower  button start here
        private void lowercaseBtn_Click(object sender, System.EventArgs e)
        {
            if (mainTxtBox.Text != "")
            {
                string str = mainTxtBox.Text;
                previewTxtBox.Text = str.ToLower();
            }
            else
            {
                MessageBox.Show("Please Enter text in the textbox.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void rmSpaceBtn_Click(object sender, System.EventArgs e)
        {
            int  strLen = mainTxtBox.Text.Length;
            for(int i=0;i<=strLen;i++)
            {
                string rm = mainTxtBox.Text.Replace("  ", " ");
                mainTxtBox.Text = rm;
                previewTxtBox.Text=rm;
            }


        }

        private void copyBtn_Click(object sender, EventArgs e)
        {
            if (mainTxtBox.Text != "")
            {
                Clipboard.SetText(previewTxtBox.Text);
            }
            else
            {
                MessageBox.Show("Please Enter text in the textbox.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void pasteBtn_Click(object sender, EventArgs e)
        {
            previewTxtBox.Clear();
            mainTxtBox.Clear();
            mainTxtBox.Text=Clipboard.GetText();
            wordCharSpacesCounter();
        }
    }
}

Output: