C# char.isupper 将数字放入字符串索引时抛出错误

C# char.isupper throwing error when a number is put in string index

我正在尝试制作一个密码验证器,但是当我输入一个数字时,这段代码抛出一个 IndexOutOfRangeException

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Password
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            string password = textBox1.Text;
            if (password.All(c => Char.IsLetterOrDigit(c)))
            {
                button4.Text = "✓";
                button4.ForeColor = System.Drawing.Color.Black;
                button4.BackColor = System.Drawing.Color.Green;
                if (password.Length >= 3 && password.Length <= 8)
                {
                    button1.Text = "✓";
                    button1.ForeColor = System.Drawing.Color.Black;
                    button1.BackColor = System.Drawing.Color.Green;

                    if (password.Any(char.IsDigit))
                    {
                        button2.Text = "✓";
                        button2.ForeColor = System.Drawing.Color.Black;
                        button2.BackColor = System.Drawing.Color.Green;
                        // Error thrown below when I input a number
                        if (char.IsUpper(password[0]) && (char.IsLower(password[8])))
                        {
                            label1.Text = "   Valid";
                            label1.ForeColor = System.Drawing.Color.Green;
                            button3.Text = "✓";
                            button3.ForeColor = System.Drawing.Color.Black;
                            button3.BackColor = System.Drawing.Color.Green;
                        }
                        else
                        {
                            label1.Text = " Invalid";
                            label1.ForeColor = System.Drawing.Color.DarkRed;
                            button2.Text = "✖";
                            button2.ForeColor = System.Drawing.Color.Black;
                            button2.BackColor = System.Drawing.Color.DarkRed;
                        }
                    }
                    else
                    {
                        label1.Text = " Invalid";
                        label1.ForeColor = System.Drawing.Color.DarkRed;
                        button3.Text = "✖";
                        button3.ForeColor = System.Drawing.Color.Black;
                        button3.BackColor = System.Drawing.Color.DarkRed;
                    }
                }
                else
                {
                    label1.Text = " Invalid";
                    label1.ForeColor = System.Drawing.Color.DarkRed;
                    button1.Text = "✖";
                    button1.ForeColor = System.Drawing.Color.Black;
                    button1.BackColor = System.Drawing.Color.DarkRed;
                }
            }
            else
            {
                button4.Text = "✖";
                button4.ForeColor = System.Drawing.Color.Black;
                button4.BackColor = System.Drawing.Color.DarkRed;
            }
        }
    }
}

只是为了让您了解为什么会遇到此问题,代码的相关部分:

  • 您检查您的密码是否严格小于 9 个字符:
    if (password.Length >= 3 && password.Length <= 8)
    {
  • 您检查您的密码是否包含数字:
        if (password.Any(char.IsDigit))
        {
  • 您尝试访问 password[8],但 无法 访问(因为它是第 9 个字符,password[0] 是第一个字符),因此你的例外:
            if (char.IsUpper(password[0]) && (char.IsLower(password[8])))
            {

简而言之:

您收到 IndexOutOfRangeException 是因为您试图访问仅包含 3 到 8 个字符的字符串的第 9 个字符。 (第三个if)
当您的输入包含数字时,您只会收到此异常,因为当您的输入包含数字时,您只会尝试访问第 9 个字符。 (第二个if)


如果您需要检查长度可变的字符串的最后一个字符,请不要像您那样对索引进行硬编码。 您可以使用 password[password.Length-1] 获取最后一个字符。