how does this code work code Digit separated by a comma (Example: 1,2,12,14 = count = 4.

TxtNumberListCount.Text &= Environment.NewLine & TxtMReadOnly.Text.Where
(Function(x) Not Char.IsWhiteSpace(x)).Count()

3 Answers

Ashok On

It is iterating through each character and counts characters which are not i believe the answer should be 9 instead of 4

Community On

this code works perfect, I've tried it well.

Private Sub BtnCountItems_Click(sender As Object, e As EventArgs) Handles BtnCountItems.Click
    Dim Cnt As Integer
    Cnt = 0
    For i = 1 To Len(TextBox1.Text)
        If Mid(TextBox1.Text, i, 1) = "," Then Cnt += 1
    Cnt += 1
End Sub

enter image description here

Mary On

Split the string into and array and get the count of elements.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Digits As String = "1,2,12,14"
    Dim NumberOfElemets As Integer = Digits.Split(","c).Count
    TextBox1.Text = NumberOfElemets.ToString
End Sub