我需要帮助处理用于验证用户姓名的程序。必须有一定程度的宽容才能接受诸如连字符,空格和撇号之类的差异。目前我正在删除无用的字符来比较字符串,但是具有相同长度的完全不同的字符的名称正好是&#d; d。我如何检查名称是否起诉类似的字符。在无用的人被移除并一起捣碎之后。
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();
}
}
}
}
}
}
答案 0 :(得分:2)
@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();
答案 1 :(得分:0)
怎么样
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
如果2个或更多字符不同,则完成操作。
答案 2 :(得分:0)
尝试如下:
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