Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using two string for a matching program like this:

    string s1= 5-4-6-+1-+1+1+3000+12+21-+1-+1-+1-+2-3-4-5-+1-+10+1-+1-+;
    string s2= 6-+1-+1+1+3000+12+21-+1-+1-+1-+1-+1-+1+1-+1-+;

as inputs. And I am going to write a Regex matching function which compare each part string between each "+" separately and calculate match percent which is number of matching occurred in each string. For example in this example we have these matching:

6

1

1

1

3000

12

21

1

1

--

1

--

1

1

Currently I am using the below function, but I think it is not optimized and using Regex may be faster.

  public double MatchPercent(string s1, string s2)
    {
            User = s1.Split('+').ToArray();
            Policy = s2.Split('+').ToArray();

     for (int i = 0; i < s1.Length - 2; i++)
            {

                int[] U = User.Split('-').Where(a => a != "").Select(n => Convert.ToInt32(n)).Distinct().ToArray();
                int[] P = Policy.Split('-').Where(a => a != "").Select(n => Convert.ToInt32(n)).Distinct().ToArray();
                var Co = U.Intersect(P);
                if (Co.Count() > 0)
                {
                    percent += 1;
                }

            }
     return Math.Round((percent) * 100 / s1.Length );



   }
share|improve this question
I don't understand what do you want to do. In your for loop, you don't use iterator value. So you always should get 98% of match or 0% of match. – Kirill Bestemyanov 1 min ago

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.