0

i have a string containing value like this

  i ={11212}
     {22121}
     {3113}
     {4122}
     {5121}  
     ......
     ......

How can i count the total no of value ?

5
  • 1
    Do you mean the number of values or the sum of the values? Commented Jun 18, 2015 at 7:07
  • Is that a string or an array of integers? Commented Jun 18, 2015 at 7:08
  • the no of value of value present in the string Commented Jun 18, 2015 at 7:08
  • i read the value from listbox and put it here and i decrare it as string Commented Jun 18, 2015 at 7:09
  • listBox.Items.Count? Commented Jun 18, 2015 at 7:15

1 Answer 1

2

You can use LinQ to count the number of brackets:

Dim Count = (From s In i Select s Where s = "{").Count

And you should rename i to something sensible in my opinion.

For when you have just any delimiter in your text and want to count single numbers you can as well use a regular expression and count the matches:

Dim InputString = "{11212}{22121}{4122}12312"
Dim rx As New System.Text.RegularExpressions.Regex("[0-9]+")
Dim NumberCount = rx.Matches(InputString).Count 'Outputs 4

This searches for occurences of numbers of any length in your input.

Sign up to request clarification or add additional context in comments.

4 Comments

is there any process ...in for each loop
if it contains only value not the " {"
instead of "{" you may put the separator that separates each value from another, whether it was a space or \n ... and add one to the count at the end.
Why? The solution is adaptable and works. This is not a "Do-my-homework" service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.