user10821918
user10821918

Reputation:

How to count a Number in textbox

I have the following number strings:

Textbox1.text / or Textbox1.Line= 1,2,3,19,29,78,48,39,40,51,53,54,69,70,71,73
Textbox2.text= / or Textbox2.Line=  1,9,3,31,29,78,45,39,40,51,59,54,69,70,71,73
textbox3.text= / or TextBox3.Line= 11,4,3,31,29,78,45,39,40,53,59,54,6974,75,76
    and Others ...

How can I make a Count that shows how many numbers from 1 to 10 are in the Textbox, how many numbers from 11-20, how many numbers from 31-40, and so on. Example: On line 1 - we will have 3 small numbers from 1 to 10 (1,2,3).

Upvotes: 0

Views: 734

Answers (2)

Precious Uwhubetine
Precious Uwhubetine

Reputation: 3007

To do this You will have to split the string into an array or list then compare the values in the array or list to the numbers you want. like this

 Dim arr1 As New List (Of String)
 arr1.AddRange(Split (TextBox1.Text, ","))
 Dim final As String
 Dim count As Integer = 0
 For Each item As String In arr1
    If CInt(item) >= 1 And  CInt(item) <= 10 Then
           count+=1
      'replace 10 with the maximum number you want and 1 with the minimum number. 
           final&=item & " "
      End If
 Next
 Msgbox("There are " & count & "numbers" & final)

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112772

You can easily convert a string containing a comma-separated list of integers into a string array like this

Dim s = "1,2,3,19,29,78,48,39,40,51,53,54,69,70,71,73"
Dim parts = s.Split(","c)

Then convert the string array into a list of integers

Dim numbers = New List(Of Integer)
For Each p As String In parts
    Dim i As Integer
    If Integer.TryParse(p, i) Then
        numbers.Add(i)
    End If
Next

Now comes the counting part. With LINQ you can write

Dim tens = From n In numbers
            Group n By Key = (n - 1) \ 10 Into Group
            Order By Key
            Select Text = $"{ 10 * Key + 1} - {10 * Key + 10}", Count = Group.Count()

This

For Each x In tens
    Console.WriteLine($"{x.Text} --> {x.Count}")
Next

Prints

1 - 10 --> 3
11 - 20 --> 1
21 - 30 --> 1
31 - 40 --> 2
41 - 50 --> 1
51 - 60 --> 3
61 - 70 --> 2
71 - 80 --> 3

Upvotes: 1

Related Questions