简体   繁体   中英

String comparison value relative to length

I need an algorithm of sorts that will return a comparison value that can be used regardless of the string's length. I have tried a few different methods to this, but quite frankly, I suck at maths.

Basically I need to get a value that finds the percentage of different characters in a string and return them to a value that can be used against a threshold.

Here are some examples:

A Sample String -> A Samlpe String

2 character missing..

A Substantially Longer Sample String - > A Substantially Longer Samlpe String

still 2 character missing..

So how could I make something that returns a similar value, regardless of string length?

I do not really understand the question, but is this what you mean?

public double Compare(string a, string b)
{
    int min = Math.Min(a.Length, b.Length);
    int max = Math.Max(a.Length, b.Length);

    double match = 0;
        
    for (int i = 0; i < min; i++)
    {
        if(a[i] == b[i])
        {
             match++;
        }
    }

    return match / max;
}

The string length doesn't matter here, you match the characters of the shortest string and return a percentage based on the longest string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM