In this C# Program, let’s create a simple visual CSharp Windows Application to count the total number of characters in an input string.
Count number of characters in a string using C#
Let’s create a form with an input field and a button and when the user will enter some string or text as input in the textbox and click to count characters button then it will count the string length and display it in a message box then we have to follow this.
Form design:
Open your Visual studio > Create A New Project In Visual C# Windows Forms Application> On Form1 take one Textbox for the input of string change its property to multi-line and one button which will display the message box that shows the count of a total number of characters in the string, name the button as Count Characters.
Now double click on Button1 control and write the below code there.
private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Total Characters are:" + textBox1.Text.Length); }
we have given a message box in the code section which will display the total number of characters of the input string from the textbox. We have given a string in the form textBox1.Text and used a string property. The length counts the number of characters in a string but includes all the white space in it.
Output:
As you can see in the below image when we type any string in the textbox then it’s showing the result by counting the characters of the string.
here is the one more example of it.
Count number of characters in a string without whitespace in C#
For this, we will make an application in which on a form we take one input field and a button. and when the user will enter some string or text in input or textbox and click to count characters button then it will count the string length without any spaces and display the result in a message box then we have to follow this.
Form design:
Open your Visual studio > Create A New Project In Visual C# Windows Forms Application > On Form1 take one Textbox for the input of string change its property to multi-line and one button name the button as Count Characters.
Now double click on Button1 and write the below code there.
private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Total no. of characters:" +(textBox1.Text.Replace(" ","").Length)); }
we have given a message box in the code section which will display the total number of characters of the input string from the textbox. We have given a string in the form textBox1.Text and used a string property. The length which counts the number of characters in a string with .length property we are using here a . Replace property which will replace the spaces by the line of code (” “,””) With this program in c# it will remove the white spaces from the string and then count the no. of characters without counting any whitespace in it.
Output:
Run the application. when we do not enter any text and click on the button it shows the output like the below image. in this image, the total no. of characters is 21 but using of . Replace property all whitespaces are removed so the output is 17.
Here is one more example of it you can clearly see that it is counting the number of characters of entered string but without counting any white spaces.
So in this way we can create programs for counting characters in the input string with space and without space in Visual C# Windows Forms Application.