Count number of characters in a string using Visual Basic (VB.NET)

In this Visual Basic Program, let’s create a simple visual basic application to count the total number of characters in a input string.

Count number of characters in a string in VB.NET

Let’s create a form with an 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 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 Basic > On Form1 take one Textbox for input of string  change its property to multi line and one button which will display the message box that shows the count of total number of characters in the string.

Now double click on Button1 control and write the below code there.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strLength As String = TextBox1.Text
        MessageBox.Show("Number of Characters: " & strLength.Length.ToString)
    End Sub

We have declared first a variable as a string that will take input of textbox and we named it strLength. Then 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. strLength variable returns the count of attributes for an element of the textbox with the help of ToString property also it will count the white space values of string.

Output:

Count number of characters in a string without space in Visual Basic(Vb)

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 Basic > On Form1 take one Textbox for input of string change its property to multi line  and one button name the button as Count Characters.

 

Now double click on the Button1 and write the below code there.

 Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim trimspaces As String = TextBox1.Text
        trimspaces = trimspaces.Replace(" ", "")

        If TextBox1.Text = "" Then
            MessageBox.Show("Please enter the text")
            TextBox1.Focus()
        Else
            MessageBox.Show("number of characters: " & trimspaces.Length)
        End If
    End Sub
End Class

The .length property is used to count the number of characters in a string and here we are using trimspaces.Replace function in which we are removing or replacing the spaces by the line of code (” “,””). With this program in vb.net it will replace the white spaces from the string and then count the no. of  characters with . length(). It allows the user to enter a text and count the characters of it without counting any space.

Output:

Run the application. when we do not enter any text and click on the button it shows the output like below.

When we enter any text and then click on the Button it will show the output like as shown in the below image.

Here you can clearly see that it is counting the number of characters of entered string but without counting any spaces.

So in this way we can create programs for counting of characters in input string with space and without space in VB.NET.