Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何验证字符串中相同的重复大小写字符?_C#_String_Validation - Fatal编程技术网

C# 如何验证字符串中相同的重复大小写字符?

C# 如何验证字符串中相同的重复大小写字符?,c#,string,validation,C#,String,Validation,我发现这段代码有两个问题-;^: 1如果不满足条件1,则显示两个验证消息框;我只希望在每个if块中显示给定条件的一个或另一个 2如何验证块2是否为字符串中的相同大小写字符 public partial class Substrings : Form { public Substrings() { InitializeComponent(); } private void buttonGo_Click(object sender, EventArgs

我发现这段代码有两个问题-;^:

1如果不满足条件1,则显示两个验证消息框;我只希望在每个if块中显示给定条件的一个或另一个

2如何验证块2是否为字符串中的相同大小写字符

public partial class Substrings : Form
{
    public Substrings()
    {
        InitializeComponent();
    }
    private void buttonGo_Click(object sender, EventArgs e)
    {
        //validate input for 5 characters and same characters with Equals method
        if (textBox3.TextLength != 5)
        {
            MessageBox.Show("The string must have exactly 5 characters, try again");
            textBox3.Clear();
            textBox3.Focus();
        }
        //validate only unique characters in string
        if (textBox3.Text.Distinct().Count() == 5)
        {
            listBox1.Items.Add(textBox3.Text.Substring(0, 1)); //p
            listBox1.Items.Add(textBox3.Text.Substring(1, 1)); //o
            listBox1.Items.Add(textBox3.Text.Substring(2, 1)); //w
            listBox1.Items.Add(textBox3.Text.Substring(3, 1)); //e
            listBox1.Items.Add(textBox3.Text.Substring(4, 1)); //r
            listBox1.Items.Add(textBox3.Text.Substring(0, 2)); //Po
            listBox1.Items.Add(textBox3.Text.Substring(1, 2)); //ow
            listBox1.Items.Add(textBox3.Text.Substring(2, 2)); //we
            listBox1.Items.Add(textBox3.Text.Substring(3, 2)); //er
            listBox1.Items.Add(textBox3.Text.Substring(0, 3)); //Pow
            listBox1.Items.Add(textBox3.Text.Substring(1, 3)); //owe
            listBox1.Items.Add(textBox3.Text.Substring(2, 3)); //wer
            listBox1.Items.Add(textBox3.Text.Substring(0, 4)); //Powe
            listBox1.Items.Add(textBox3.Text.Substring(1, 4)); //ower
            listBox1.Items.Add(textBox3.Text.Substring(0, 5)); //Power
        }
        else
            MessageBox.Show("The string must have distinct, non-repeating characters");
    }
    private void textBox3_TextChanged(object sender, EventArgs e)
    {
    }
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
    private void buttonExit_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

对于第一个部分,只需使用elseif,那么第二个块将仅在第一个块不存在时执行

对于第二部分,您可以使用ToLower或ToUpper,以便在相同的情况下比较所有字符

因此,结合这两个变化:

else if (textBox3.Text.ToUpper().Distinct().Count() == 5)

您要验证文本框的字符数不应超过5个,然后还要验证文本框是否需要具有唯一字符。你可以试试这个:

//validate input for 5 characters and same characters with Equals method
if (textBox3.Length != 5)
{
    MessageBox.Show("The string must have exactly 5 characters, try again");
    textBox3.Clear();
    textBox3.Focus();
}
//validate only unique characters in string
else if (textBox3.Text.ToLower().Distinct().Count() != textBox3.Text.Length)
{
    MessageBox.Show("The string must have distinct, non-repeating characters");
}
else
{
    // Do your stuffs here
}

编写textBox3.Text.ToLower.Distinct.Count==5@Dusan它还具有实际工作的额外优势!我想我的大脑已经衰退了。你需要一个合格的比较员。我已经改变了答案。而且,列表框的填充需要进行认真的重构。@Dusan我也会接受任何重构建议!谢谢