检查重复单词的字段数组
Checking an array of fields for a repeated word
我有一个单词数组和一个输入字段数组,输入一个单词然后单击按钮后,将在单词数组中执行搜索。还应检查单词是否重复,即如果单词已经写过,则不应再次使用。我写了这段代码,但这里是如何检查我不知道的单词的重复,请告诉我如何完成?
string[] Array_words= new string[] { "Dot", "Life", "World", "Tree"};
public void Done()
{
foreach (InputField inputField in InputFields)
{
string[] ArrayW= inputField.text.Split(' ');
for (int i = 0; i < Array_words.Length; i++)
{
foreach (string s in ArrayW)
{
if (s.Contains(Array_words[i]))
Debug.LogFormat($"OK");
}
}
}
}
请告诉我如何实现对所有字段是否为空的检查,即如果所有字段都为空,则他在日志中写入一条消息,这样试过,但仍然输出一条消息,即使它写在一个字段中。
foreach (InputField inputField1 in InputFields)
{
t=inputField1.text;
}
if (string.IsNullOrEmpty(t))
{
Debug.LogFormat($"field is Empty!");
}
对每个字段进行重复数据删除:
string[] Array_words= new string[] { "Dot", "Life", "World", "Tree"};
public void Done()
{
//dedupe all fields
var hs = new HashSet<string>();
foreach (InputField inputField in InputFields)
hs.UnionWith(inputField.text.Trim().Split());
//prohibit all fields blank
if(hs.Count == 1 && hs.First() == "") throw new InvalidOperationException("All fields are blank");
//check all entered words are present in Array_words
var allOk = hs.All(Array_words.Contains);
}
请注意,C# 默认区分大小写。 dot
和 Dot
是不同的东西
我有一个单词数组和一个输入字段数组,输入一个单词然后单击按钮后,将在单词数组中执行搜索。还应检查单词是否重复,即如果单词已经写过,则不应再次使用。我写了这段代码,但这里是如何检查我不知道的单词的重复,请告诉我如何完成?
string[] Array_words= new string[] { "Dot", "Life", "World", "Tree"};
public void Done()
{
foreach (InputField inputField in InputFields)
{
string[] ArrayW= inputField.text.Split(' ');
for (int i = 0; i < Array_words.Length; i++)
{
foreach (string s in ArrayW)
{
if (s.Contains(Array_words[i]))
Debug.LogFormat($"OK");
}
}
}
}
请告诉我如何实现对所有字段是否为空的检查,即如果所有字段都为空,则他在日志中写入一条消息,这样试过,但仍然输出一条消息,即使它写在一个字段中。
foreach (InputField inputField1 in InputFields)
{
t=inputField1.text;
}
if (string.IsNullOrEmpty(t))
{
Debug.LogFormat($"field is Empty!");
}
对每个字段进行重复数据删除:
string[] Array_words= new string[] { "Dot", "Life", "World", "Tree"};
public void Done()
{
//dedupe all fields
var hs = new HashSet<string>();
foreach (InputField inputField in InputFields)
hs.UnionWith(inputField.text.Trim().Split());
//prohibit all fields blank
if(hs.Count == 1 && hs.First() == "") throw new InvalidOperationException("All fields are blank");
//check all entered words are present in Array_words
var allOk = hs.All(Array_words.Contains);
}
请注意,C# 默认区分大小写。 dot
和 Dot
是不同的东西