简体   繁体   English

我如何检查字符串以查看所有字符是否都是整数,长度为 8 个字符并为每个字符抛出一个参数?

[英]How do i check string to see if all characters are integers, that it is 8 characters in length and throw an argument for each?

Class student and the main are just for reference.. In the try catch block there are two separate s1.SNum = "string" that should each throw a separate argument but both throw the same one every time and I'm not figuring it out. Class student 和 main 仅供参考。在 try catch 块中有两个单独的 s1.SNum = "string" 应该每个都抛出一个单独的参数,但每次都抛出相同的参数,我没有弄清楚.

class Student
    {
       public string FirstName { get; private set; }
       public string LastName { get; private set; }
       public Date BirthDate { get; private set; }

       public Date catalog;
       public string sNum;
     }

This is the code im having trouble with.这是我遇到问题的代码。 I want to have two sets that throw exceptions for sNum.我想有两组为 sNum 抛出异常。 One for not having all digits and one for not having a length of eight.一个表示没有所有数字,一个表示长度不为八。

public string SNum
        {
            get
            {
                return sNum;
            }

            set
            {
            foreach (char c in sNum)
            {
                int count = 0;
                char charToCount = ',';
                bool result = (SNum.All(Char.IsDigit));

                if (result == false)
                {
                    throw new ArgumentOutOfRangeException(nameof(SNum), value,
                    $"{nameof(SNum)} characters are not all integers.");
                }

                if (c == charToCount)
                {
                    count++;
                }
                else if (count != 8)       BOTH WILL THROW THIS ARGUMENT RIGHT NOW
                {
                    throw new ArgumentOutOfRangeException(nameof(SNum), value,
                    $"{nameof(SNum)} length is not eight.");
                }
            }
        }

And the Main Class for reference to the try catch..Even if I'm checking for any letters s1.SNum = "158945K9";和主要的 Class 参考 try catch..即使我正在检查任何字母s1.SNum = "158945K9"; will still throw the same exception as s1.SNum = "1234567";仍然会抛出与s1.SNum = "1234567";

            // attempt to assign invalud sNum length
            try
            {
                s1.SNum = "1234567";  
            }
            catch (ArgumentOutOfRangeException ex)
            {
                Console.WriteLine($"{ex.Message}\n");
            }

            // attempt to assign invalud sNum character
            try
            {
                s1.SNum = "158945K9";
            }
            catch (ArgumentOutOfRangeException ex)
            {
                Console.WriteLine($"{ex.Message}\n");
            }

If all you care about is that the value is all digits and is 8 characters long then the implementation is far simpler than you have:如果您只关心该值是所有数字并且长度为 8 个字符,那么实现比您所拥有的要简单得多:

set
{
    if (value.Any(ch => !char.IsDigit(ch))
        throw new ArgumentException("All characters must be digits.");

    if (value.Length != 8)
        throw new ArgumentException("Value must be eight characters in length.");

    sNum = value;
}

It seems like there's more to it than that though, but you haven't explained the rest.似乎还有更多的东西,但你还没有解释 rest。 If you want to allow commas and they don't get included in the length calculation then that changes to something like this:如果您想允许逗号并且它们不包含在长度计算中,那么它将变为如下所示:

set
{
    if (value.Any(ch => ch != ',' && !char.IsDigit(ch))
        throw new ArgumentException("All characters must be digits or commas.");

    if (value.Count(ch => ch != ',') != 8)
        throw new ArgumentException("Value must contain eight digits.");

    sNum = value;
}

You might prefer to use All rather than Any , which will also work in both cases and avoids the double negation in the second code snippet, but still requires the exact same number of characters in each case:您可能更喜欢使用All而不是Any ,这在这两种情况下也都有效,并避免了第二个代码片段中的双重否定,但在每种情况下仍需要完全相同数量的字符:

set
{
    if (!value.All(ch => char.IsDigit(ch))
        throw new ArgumentException("All characters must be digits.");

    if (value.Length != 8)
        throw new ArgumentException("Value must be eight characters in length.");

    sNum = value;
}

and:和:

set
{
    if (!value.All(ch => ch == ',' && char.IsDigit(ch))
        throw new ArgumentException("All characters must be digits or commas.");

    if (value.Count(ch => ch != ',') != 8)
        throw new ArgumentException("Value must contain eight digits.");

    sNum = value;
}

Using Any also maintains more consistency with the other if statements, but that's a very minor thing.使用Any还可以与其他if语句保持更多的一致性,但这是一件非常小的事情。

Note also that I have thrown an ArgumentException rather than an ArgumentOutOfRangeException .另请注意,我抛出了ArgumentException而不是ArgumentOutOfRangeException There's no specific range of valid values in this case so you should not be throwing an exception that indicates that there is.在这种情况下,没有特定范围的有效值,因此您不应抛出表明存在的异常。 If there was a range then you could describe it using a minimum and maximum value.如果有一个范围,那么您可以使用最小值和最大值来描述它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM