简体   繁体   English

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

[英]Compare similarity of two Strings for name verification

I need help with a program that im using to verify a user's names. 我需要用于验证用户名的程序的帮助。 there has to be a certain level of leniency to accept differences like hyphens, spaces and apostrophes. 必须有一定程度的宽大处理才能接受诸如连字符,空格和撇号之类的差异。 Currently im removing useless characters to compare the strings but names of completely different characters with the same length are being Ok'd. 目前,我正在删除无用的字符来比较字符串,但是可以确定完全相同长度的完全不同的字符的名称。 how do i check to see the names are suing similar characters. 如何检查名称是否使用相似字符。 after the useless ones have been removed and mushed together. 将无用的东西移走并捣毁后。

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 gave a great solution for determining character difference, but I just want to show you a much better way to strip characters than stringing .Replace() over and over and over again: @TestWell为确定字符差异提供了一个很好的解决方案,但是我只想向您展示一种比一遍又一遍地串起.Replace()更好的剥离字符的方法:

You can just add/remove chars from this array: 您可以仅从以下数组添加/删除字符:

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

usage: 用法:

 firstname = cleanString(firstname);

or as an extension method: 或作为扩展方法:

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();
    }
 }
}

usage: 用法:

firstname = firstname.CleanString();

How about 怎么样

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;
}

To return number of different characters. 返回不同字符的数量。 You could then say 然后你可以说

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

Completing an action if 2 or more characters are different. 如果两个或更多字符不同,则完成一个动作。

try as following : 尝试如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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