1

What is the best way to count the number of numeric elements within a string where the elements are delimited by '/'?

For example, in the string "OL/12/15/NR/17/21", we have 4 numeric elements. The only non-numeric (i.e. alpha) elements that can be found in the array are "OL" (off ladder) and "NR" (no result).

This is my solution but I suspect that there are better, more efficient algorithms:

int cnt = 0;
string sampleString = "OL/12/15/NR/17/21";
string[] sampleArray = sampleString.split('/');

foreach (string element in sampleArray)
    if (element != "OL" && element != "NR")
        cnt++;
1
  • try regular expression that is good way to find easily, try my answer ..there is no need to hard code any thing Commented Nov 21, 2017 at 5:40

4 Answers 4

4

Here is an alternate option with LINQ:

string[] sampleArray = sampleString.Split(new Char[]{'/'},StringSplitOptions.RemoveEmptyEntries);
int strCount = sampleArray.Count(x=> x!= "OL" && x != "NR");

This would be shorter but can't say this one is efficient, Here you can find a working example to prove this:

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

3 Comments

@RufusL: Yep you are correct, I have added a link to prove your comment
No doubt this is efficient way but still that's not a good approach to your problem. Use Regular Expression instead.
I did a quick Stopwatch test & found this is faster than regex surprisingly.
3

Use regex to match numbers:

string sampleString = "OL/12/15/NR/17/21";
int count = Regex.Matches(sampleString,@"\b\d+\b").Count;

7 Comments

how come its "\d+" that is stand for digit, you can check it here : regex101.com/r/iI1wQ7/3 , Please correct me if i am wrong some where
This regular expression does not cater for the scenario where the first element in the string is numeric.
You're right. But this answer is just to give you a better approach. However I updated the answer.
@MUT - sorry to say but you can go to link i given and check it , you can run code in visual studio and chck...bet not to put -1 without checking it...just to get more points
Output of my program aslo given by me ..run my code in visual studio and check ...and let me know if it matching alphanumeric ....
|
0

I will prefer some generic method like this:

int cnt = 0;
string sampleString = "OL/12/15/NR/17/21";
string[] sampleArray = sampleString.split('/');
int numValue;

foreach (string element in sampleArray)
    if (Int32.TryParse(element , out numValue))
        cnt++;

Comments

0

you can make use of regular expression , below is full code for you

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"(\d+)";
        string input = @"OL/12/15/NR/17/21";

        foreach (Match m in Regex.Matches(input, pattern))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

Output of my working code

  • '12' found at index 3.
  • '15' found at index 6.
  • '17' found at index 12.
  • '21' found at index 15.

here is demo for the same : https://regex101.com/r/iI1wQ7/3

4 Comments

Your pattern will also matches the number with alphabets. e.g @"OL/12/15/NR1234/17/21". In this case number of matches is 5 but it should be 4
@MUT - how come its "\d+" that is stand for digit, you can check it here : regex101.com/r/iI1wQ7/3
It matches the digit but \d is not equivalent to [0-9].
@MUT - sorry to say but you can go to link i given and check it , you can run code in visual studio and chck...bet not to put -1 without checking it...just to get more points

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.