[英]how to check whether the characters in a word is mentioned in group of various character representing text boxes
我有七個隨機字符生成的文本框(在圖片的上方分組)和一個用於輸入單詞的文本框(在下方)。 現在驗證是如果我輸入了以下單詞:
1)狐狸:它不應包含此單詞,因為它必須包含至少一次為黃色的中間單詞
string holdWord = textBox1.Text; //
char charToCheck = Convert.ToChar(textBox3.Text); // 'a'
bool result = holdWord.Contains( charToCheck ); //false
我使用此代碼,它可以工作。
2)胖:因為上面的任何文本框中均未提及t,因此不應使用該單詞,並且應返回該字母作為錯誤,表明該字母不存在。僅盒子
所以2)是您唯一的開放要求。
// presuming textBox1-textBox6 are the textboxes for the letters
// and textBox7 is the TextBox "Type your word"
TextBox[] textBoxes = { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6 };
string wordTyped = textBox7.Text;
var notContainedLetters = wordTyped
.Where(c => !textBoxes.Any(txt => txt.Text.Contains(c)));
string notContained = String.Concat( notContainedLetters );
MessageBox.Show("These letters are in no textbox: " + notContained);
您需要using System.Linq
添加LINQ查詢。
您可以使用notContained.Length > 0
或Enumerable.Any
來檢查是否至少缺少一個字母:
bool anyMissing = notContainedLetters.Any();
char[] originalLetters = { 'F', 'O', 'D','J', 'X'}; // Add TextBox lettes in this array
string enteredWord = "FAT"; //User Entered Word
foreach (char letter in enteredWord)
{
// check if letter can be found withinenteredWord
if (!originalLetters.Contains(letter))
{
//// Do Something
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.