[英]Comparing two string in c#
I have a program that needs to compare any given string with a predefined string and determine if an insertion error, deletion error, transposition or substitution error was made. 我有一个程序,需要将任何给定的字符串与预定义的字符串进行比较,并确定是否发生了插入错误,删除错误,移位或替换错误。
For example, if the word dog was presented to the user and the user submits dogs or doge , it should notify the user that an insertion error has been made. 例如,如果将单词“ 狗”呈现给用户,并且用户提交了“ 狗”或“ doge” ,则它应通知用户已发生插入错误。
How do I go about this? 我该怎么办?
You probably need to write a method for each of the individual error types to see if it's an error, like: 您可能需要为每种单独的错误类型编写一个方法,以查看是否为错误,例如:
bool IsInsertionError(string expected, string actual) {
// Maybe look at all of the chars in expected, to see if all of them
// are there in actual, in the correct order, but there's an additional one
}
bool IsDeletionError(string expected, string actual) {
// Do the reverse of IsInsertionError - see if all the letters
// of actual are in expected, in the correct order,
// but there's an additional one
}
bool IsTransposition(string expected, string actual) {
// This one might be a little tricker - maybe loop through all the chars,
// and if expected[i] != actual[i], check to see if
// expected[i+1] = actual[i] and actual[i-1]=expected[i]
// or something like that
}
Once you build out all the individual rules, and you first check for regular equality, fire each of them off one at a time. 一旦建立了所有单独的规则,并首先检查规则是否相等,就一次将它们中的每一个解雇。
You've got to just break problems like this down into small components, then once you have a bunch of easy problems, solve them one at a time. 您必须将这样的问题分解为小组件,然后一旦遇到一堆简单的问题,就一次解决它们。
Off the top of my head but I think this should get you started: 从我的头顶上跳下来,但我认为这应该可以帮助您入门:
Insertion
and Deletion
should be pretty simple; Insertion
和Deletion
应该非常简单; just check the lengths of the strings. 只需检查字符串的长度即可。
if(originalString.Length > newString.Length)
{
//deletion
}
else if(originalString.Length < newString.Length)
{
//insertion
}
To detect transposition
, check if the lengths match and if so, you could create two List<char>
from the two strings. 要检测
transposition
,请检查长度是否匹配,如果匹配,则可以从两个字符串中创建两个List<char>
。 Then check if they match using the expression below 然后使用以下表达式检查它们是否匹配
bool isTransposed = originalList.OrderBy(x => x).SequenceEquals(newList.OrderBy(y => y));
To detect substitution
, you could use the Hamming Distance and check if it's greater than 0. 要检测
substitution
,可以使用汉明距离并检查其是否大于0。
I would suggest you to create a function
which will take a parameter
as the input sting
. 我建议您创建一个将
parameter
作为input sting
的function
。 The function
would look more or less like this. 该
function
或多或少看起来像这样。 Use the function
wherever you want then. 然后,在任何需要的地方使用该
function
。
private void CheckString(string userString)
{
string predefinedString = "dog";
if (userString == predefinedString)
{
// write your logic here
}
else
{
MessageBox.Show("Incorrect word"); // notify the users about incorrect word over here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.