需要在文本框中搜索数字组合(c#)
Need to search for number combinations within textbox(c#)
我有一个带有文本框的表单,它接受 12 个数字作为输入。
我希望能够搜索 2 位、3 位、4 位和 5 位组合,并为找到的每个此类组合显示预定义的输出。
例子
在文本框中输入的文本:919982115672
输出
8211 : Some predefined text
82 : Some predefined text
9821 : Some predefined text
5672 : Some predefined text
以此类推
关于如何制作预定义值数据库的一些建议也很棒
谢谢。
您正在搜索子字符串,如下所示:
string text = "919982115672";
Dictionary<string, string> combiTexts = new Dictionary<string, string>
{
{"8211","Some predefined text"},
{"82","Some predefined text"},
{"9821","Some predefined text"},
{"5672","Some predefined text"}
};
var matchingSubstrings = text.getAllSubstrings()
.Where(combiTexts.ContainsKey)
.Select(str => string.Format("{0} : {1}", str, combiTexts[str]));
使用此扩展从字符串中提取所有子字符串:
public static IEnumerable<string> getAllSubstrings(this string word)
{
return from charIndex1 in Enumerable.Range(0, word.Length)
from charIndex2 in Enumerable.Range(0, word.Length - charIndex1 + 1)
where charIndex2 > 0
select word.Substring(charIndex1, charIndex2);
}
输出
foreach(string output in matchingSubstrings)
Console.WriteLine(output);
9821 : Some predefined text
82 : Some predefined text
8211 : Some predefined text
5672 : Some predefined text
在文本框更改事件中,只需将文本框中的文本与您要搜索的预定义字符串进行比较(因此对于用户添加的每个数字,您的程序将在现场检查是否找到任何组合)
试试这个:
string Input = "919982115672";
Console.Write(Search(Input));
public string Search(string Input)
{
Dictionary<string, string> PreDefinedSearchAndTexts = new Dictionary<string, string>();
PreDefinedSearchAndTexts.Add("8211", "Hello");
PreDefinedSearchAndTexts.Add("82", "My name is inigo montya");
PreDefinedSearchAndTexts.Add("9821", "You killed my father");
PreDefinedSearchAndTexts.Add("5672", "Prepare to die");
StringBuilder sb = new StringBuilder();
foreach(string Key in PreDefinedSearchAndTexts.Keys)
{
if(Input.Contains(Key))
{
sb.Append(Key).Append(" : ").AppendLine(PreDefinedSearchAndTexts[Key]);
}
}
return sb.ToString();
}
结果:
8211 : Hello
82 : My name is inigo montya
9821 : You killed my father
5672 : Prepare to die
我有一个带有文本框的表单,它接受 12 个数字作为输入。
我希望能够搜索 2 位、3 位、4 位和 5 位组合,并为找到的每个此类组合显示预定义的输出。
例子
在文本框中输入的文本:919982115672
输出
8211 : Some predefined text
82 : Some predefined text
9821 : Some predefined text
5672 : Some predefined text
以此类推
关于如何制作预定义值数据库的一些建议也很棒
谢谢。
您正在搜索子字符串,如下所示:
string text = "919982115672";
Dictionary<string, string> combiTexts = new Dictionary<string, string>
{
{"8211","Some predefined text"},
{"82","Some predefined text"},
{"9821","Some predefined text"},
{"5672","Some predefined text"}
};
var matchingSubstrings = text.getAllSubstrings()
.Where(combiTexts.ContainsKey)
.Select(str => string.Format("{0} : {1}", str, combiTexts[str]));
使用此扩展从字符串中提取所有子字符串:
public static IEnumerable<string> getAllSubstrings(this string word)
{
return from charIndex1 in Enumerable.Range(0, word.Length)
from charIndex2 in Enumerable.Range(0, word.Length - charIndex1 + 1)
where charIndex2 > 0
select word.Substring(charIndex1, charIndex2);
}
输出
foreach(string output in matchingSubstrings)
Console.WriteLine(output);
9821 : Some predefined text
82 : Some predefined text
8211 : Some predefined text
5672 : Some predefined text
在文本框更改事件中,只需将文本框中的文本与您要搜索的预定义字符串进行比较(因此对于用户添加的每个数字,您的程序将在现场检查是否找到任何组合)
试试这个:
string Input = "919982115672";
Console.Write(Search(Input));
public string Search(string Input)
{
Dictionary<string, string> PreDefinedSearchAndTexts = new Dictionary<string, string>();
PreDefinedSearchAndTexts.Add("8211", "Hello");
PreDefinedSearchAndTexts.Add("82", "My name is inigo montya");
PreDefinedSearchAndTexts.Add("9821", "You killed my father");
PreDefinedSearchAndTexts.Add("5672", "Prepare to die");
StringBuilder sb = new StringBuilder();
foreach(string Key in PreDefinedSearchAndTexts.Keys)
{
if(Input.Contains(Key))
{
sb.Append(Key).Append(" : ").AppendLine(PreDefinedSearchAndTexts[Key]);
}
}
return sb.ToString();
}
结果:
8211 : Hello
82 : My name is inigo montya
9821 : You killed my father
5672 : Prepare to die