比较两个字符串的相似度以进行名称验证

本文关键字:验证 相似 字符串 两个 比较 | 更新日期: 2023-09-27 17:54:11

我需要一个程序的帮助,我用它来验证用户的名字。必须有一定程度的宽容来接受连字符、空格和撇号等差异。目前,我正在删除无用的字符来比较字符串,但具有相同长度的完全不同字符的名称正在被Ok。我如何检查名字是否使用相似的字符?在无用的部分被移走并混合在一起之后。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace name_v
{
    class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("enter first name");
        string firstname = Console.ReadLine();
        Console.WriteLine("Enter 2nd name");
        string secondname = Console.ReadLine();
        if (firstname == secondname)
        {
            Console.WriteLine("Names are exactly the same");
            Console.ReadLine();
        }

        else
        {
            Console.WriteLine("press enter to Compare");
            Console.ReadLine();
            int numFirstName = firstname.Length;
            int numSecondName = secondname.Length;
            Console.WriteLine("# in 1st = " + numFirstName);
            Console.WriteLine("# in 2nd = " + numSecondName);
            Console.ReadLine();                                                                                                                                                   
            firstname = firstname.Replace(" ", "").Replace("-", "").Replace("'", "").Replace(".", "").ToUpper();
            secondname = secondname.Replace(" ", "").Replace("-", "").Replace("'", "").Replace(".", "").ToUpper();

            Console.WriteLine("Names to be compared as");
            Console.WriteLine(firstname);
            Console.WriteLine(secondname);
            numFirstName = firstname.Length;
            numSecondName = secondname.Length;
            Console.WriteLine("# in 1st = " + numFirstName);
            Console.WriteLine("# in 2nd = " + numSecondName);
            Console.ReadLine();
            int nameLengthDif = numFirstName - numSecondName;     
            if (firstname == secondname)
            {
                Console.WriteLine("Names are the same");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("Names are not the same");
                Console.ReadLine();
                        if (nameLengthDif < 3)
                        {
                            Console.WriteLine("But Close enough");
                            Console.ReadLine();
                        }
                        else
                        {
                            Console.WriteLine("And not Close enough");
                            Console.ReadLine();
                        }
            }

        }
    }

}
}

比较两个字符串的相似度以进行名称验证

@TestWell给出了确定字符差异的一个很好的解决方案,但我只是想向您展示一种比字符串更好的方法来删除字符。replace()反复使用:

你可以从这个数组中添加/删除字符:

private char[] invalid = new char[] {' ','-','_','.'};
private static string cleanString(string input)
{    
   return new string(input.Where(x => !invalid.Contains(x)).ToArray()).ToLower();
}

用法:

 firstname = cleanString(firstname);

或作为扩展方法:

namespace CustomExtensions
{
 public static class StringExtension
 {
    private static char[] invalid = new char[] {' ','-','_','.'};
    public static string CleanString(this string y)
    {
      return new string(y.Where(x => !invalid.Contains(x)).ToArray()).ToLower();
    }
 }
}

用法:

firstname = firstname.CleanString();

尝试如下:

char[] firstName = firstName.ToCharArray();
char[] lastName = lastName.ToCharArray();
char[] res = firstName.Except(lastName).ToArray();
if(res.Length < 1)
{
  res = lastName.Except(firstName).ToArray();
}
nameLengthDif = res.Length

private static int GetDifferenceCount(string firstName, string lastName)
{
    int differences;
    string longestString = firstName;
    if (longestString.Length < lastName.Length)
        longestString = lastName;
    for (int i = 0; i < longestString.Length; i++)
    {
        try
        {
            if (firstName.Substring(i, 1) != lastName.Substring(i, 1))
                differences++;
        }
        catch
            differences++;
    }
    return differences;
}

返回不同字符的数目。然后可以说

if (GetDifferenceCount(firstName, lastName) >= 2)
    // Do something

完成一个动作,如果两个或两个以上的字符不同。