C# 字符/字符串比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12906297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Char/String comparison
提问by Quigg15405
I'm trying to have a suggestion feature for the search function in my program eg I type janw doe in the search section and it will output NO MATCH - did you mean jane doe? I'm not sure what the problem is, maybe something to do with char/string comparison..I've tried comparing both variables as type char eg char temp -->temp.Contains ...etc but an error appears (char does not contain a definition for Contains). Would love any help on this! 8)
我正在尝试为我的程序中的搜索功能提供一个建议功能,例如我在搜索部分输入 janw doe,它会输出 NO MATCH - 你是说 jane doe 吗?我不确定问题是什么,可能与字符/字符串比较有关..我尝试将两个变量作为字符类型进行比较,例如 char temp -->temp.Contains ...等,但出现错误(char不包含对包含的定义)。希望对此有任何帮助!8)
if (found == false)
{
Console.WriteLine("\n\nMATCH NOT FOUND");
int charMatch = 0, charCount = 0;
string[] checkArray = new string[26];
//construction site /////////////////////////////////////////////////////////////////////////////////////////////////////////////
for (int controlLoop = 0; controlLoop < contPeople.Length; controlLoop++)
{
foreach (char i in userContChange)
{
charCount = charCount + 1;
}
for (int i = 0; i < userContChange.Length; )
{
string temp = contPeople[controlLoop].name;
string check=Convert.ToString(userContChange[i]);
if (temp.Contains(check))
{
charMatch = charMatch + 1;
}
}
int half = charCount / 2;
if (charMatch >= half)
{
checkArray[controlLoop] = contPeople[controlLoop].name;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
Console.WriteLine("Did you mean: ");
for (int a = 0; a < checkArray.Length; a++)
{
Console.WriteLine(checkArray[a]);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
回答by Alan
A string is made up of many characters. A character is a primitive, likewise, it doesn't "contain" any other items. A string is basically an array of characters.
一个字符串由许多字符组成。字符是原始的,同样,它不“包含”任何其他项目。字符串基本上是一个字符数组。
For comparing string and characters:
用于比较字符串和字符:
char a = 'A';
String alan = "Alan";
Debug.Assert(alan[0] == a);
Or if you have a single digit string.. I suppose
或者如果你有一个数字字符串..我想
char a = 'A';
String alan = "A";
Debug.Assert(alan == a.ToString());
All of these asserts are true
所有这些断言都是真的
But, the main reason I wanted to comment on your question, is to suggest an alternative approach for suggesting "Did you mean?". There's an algorithm called Levenshtein Distance which calculates the "number of single character edits" required to convert one string to another. It can be used as a measure of how close two strings are. You may want to look into how this algorithm works because it could help you.
但是,我想对您的问题发表评论的主要原因是建议另一种建议“您是说吗?”的方法。有一种称为 Levenshtein 距离的算法,它计算将一个字符串转换为另一个字符串所需的“单字符编辑次数”。它可以用来衡量两个字符串的接近程度。你可能想研究一下这个算法是如何工作的,因为它可以帮助你。
Here's an applet that I found which demonstrates: Approximate String Matching with k-differences
这是我发现的一个小程序,它演示了:Approximate String Matching with k-differences
Also the wikipedia link Levenshtein distance
还有维基百科链接Levenshtein distance
回答by T-moty
Char type cannot have .Contains()because is only 1 char value type.
Char 类型不能有,.Contains()因为只有 1 个 char 值类型。
In your case (if i understand), maybe you need to use .Equals()or the ==operator.
在您的情况下(如果我理解),也许您需要使用.Equals()或==操作员。
Note: for compare String correctly, use .Equals(),
the ==operator does not work good in this case because Stringis reference type.
注意:为了正确比较字符串,请使用.Equals(),==在这种情况下,运算符不起作用,因为它String是引用类型。
Hope this help!
希望这有帮助!
回答by aspark
chartype dosen't have the Contains()method, but you can use iit like this: 'a'.ToString().Contains(...)
chartype 没有这个Contains()方法,但你可以像这样使用 iit:'a'.ToString().Contains(...)
if do not consider the performance, another simple way:
如果不考虑性能,另一种简单的方法:
var input = "janw doe";
var people = new string[] { "abc", "123", "jane", "jane doe" };
var found = Array.BinarySearch<string>(people, input);//or use FirstOrDefault(), FindIndex, search engine...
if (found < 0)//not found
{
var i = input.ToArray();
var target = "";
//most similar
//target = people.OrderByDescending(p => p.ToArray().Intersect(i).Count()).FirstOrDefault();
//as you code:
foreach (var p in people)
{
var count = p.ToArray().Intersect(i).Count();
if (count > input.Length / 2)
{
target = p;
break;
}
}
if (!string.IsNullOrWhiteSpace(target))
{
Console.WriteLine(target);
}
}

