Home > Blockchain >  Checking an array of fields for a repeated word
Checking an array of fields for a repeated word

Time:03-30

I have an array of words and an array of input fields, after entering a word and then clicking on the button, a search will be performed in the array of words. It should also be checked that the words are not repeated, i.e. if the word has already been written, then it should not be used again. I wrote this code, but here's how to make it checked for repetition of the word I don't know, please tell me how it can be done?

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");

            }
        }
    }
   
}

And please tell me how you can implement a check for the emptiness of all fields, i.e. if all the fields are empty, then he writes a message in the Log, tried like this, but still outputs a message even if it is written in one field.

 foreach (InputField inputField1 in InputFields)
    {
     t=inputField1.text;
    }
    if (string.IsNullOrEmpty(t))
    {
        Debug.LogFormat($"field is Empty!");

    }

CodePudding user response:

Deduping across every field:

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);
   
}

Note that C# is case sensitive by default. dot and Dot are different things

  • Related