是否有 Int.isWholeNumber() 函数或类似的函数?

发布于 2024-11-05 15:09:14 字数 213 浏览 2 评论 0原文

我需要检查输入是否为 int。是否有与 String.IsNullOrEmpty() 类似的函数,例如 Int.isWholeNumber() 函数?

有没有办法仅在 if() 语句中验证这一点,而不必事先声明 int ? (正如您需要使用 TryParse() 所做的那样)

编辑
我需要验证区号(五个数字)

I need to check if an input is an int or not. Is there a similar function to the String.IsNullOrEmpty(), like an Int.isWholeNumber() function?

Is there a way to validate this inside the if() statement only, without having to declare an int before? (As you need to do with TryParse())

EDIT
I need to validate an area code (five numbers)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

剩一世无双 2024-11-12 15:09:14

我不相信 BCL 中有任何这样的方法,但编写一个很容易(我假设您真的在谈论字符串是否可以解析为整数):

public static bool CanParse(string text)
{
    int ignored;
    return int.TryParse(text, out ignored);
}

添加接受 IFormatProvider值等,根据您的需要。假设这是为了验证,您可能希望扩展它以允许指定一系列有效值...

如果您正在进行大量自定义验证,您可能希望查看 Fluent Validation 项目。

I don't believe there's any such method within the BCL, but it's easy to write one (I'm assuming you're really talking about whether a string can be parsed as an integer):

public static bool CanParse(string text)
{
    int ignored;
    return int.TryParse(text, out ignored);
}

Add overloads accepting IFormatProvider values etc as you require them. Assuming this is for validation, you might want to expand it to allow a range of valid values to be specified as well...

If you're doing a lot of custom validation, you may well want to look at the Fluent Validation project.

眼眸 2024-11-12 15:09:14

我从您的评论中了解到,您的实际问题是“如何确定该字符串是否包含有效的瑞典邮政编码?”瑞典邮政编码是一个不以零开头的五位数字。如果这是您实际需要解决的问题,那么解决该问题。我不会尝试将字符串转换为整数然后检查整数,而是简单地编写检查:

  • 字符串是否有五个字符长?如果没有,请拒绝。
  • 字符串的第一个字符是 1、2、3、4、5、6、7、8 或 9?如果没有,请拒绝。
  • 字符串的第二个、第三个、第四个和第五个字符是 0、1、2、3、4、5、6、7、8 或 9?如果没有,请拒绝。

就这么简单。如果您永远不会对其进行数学运算,那么首先不要将其转换为整数。

这种方法将进一步推广到更复杂的形式。据我所知,瑞典邮政编码通常以“SE-12 345”的形式书写,即带有前缀“SE-”,数字二和三之间有一个空格。编写处理该格式的整数验证例程将非常困难,但编写字符串验证例程却很简单。

更一般地说,这说明了一些关于编写问题的好建议。 提出有关您实际必须解决的问题的问题。您假设了一个解决方案——将字符串解析为整数——然后开始询问有关您假设的解决方案的问题。这会自动阻止任何人针对您的实际问题提供建议。也许阅读本文的人已经开发了一个邮政编码验证软件库;如果他们有,他们永远不会知道从你最初的问题中告诉你这一点。

I gather from your comments that your actual problem is "how do I determine if this string contains a valid Swedish postal code?" A Swedish postal code is a five digit number not beginning with a zero. If that's the problem you actually have to solve, then solve that problem. Rather than trying to convert the string to an integer and then check the integer, I would simply write checks that say:

  • is the string five characters long? If not, reject it.
  • is the first character of the string 1, 2, 3, 4, 5, 6, 7, 8 or 9? If not, reject it.
  • are the second, third, fourth and fifth characters of the string 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9? If not, reject it.

Simple as that. If you're never going to do math on it, don't convert it to an integer in the first place.

This approach will further generalize to more complex forms. Swedish postal codes, I gather, are often written in the form "SE-12 345", that is, with the prefix "SE-" and a space between digits two and three. It's going to be awfully hard to write an integer-validating routine that deals with that format, but writing a string-validating routine is straightforward.

More generally, this illustrates some good advice for writing questions. Ask a question about the problem you actually must solve. You assumed a solution -- parse the string as an integer -- and then started asking questions about your assumed solution. That automatically precludes anyone from giving advice that is specific to your real problem. Maybe someone reading this has already developed a library of postal-code validating software; if they have, they'd never know to tell you about it from your original question.

云朵有点甜 2024-11-12 15:09:14

您可以通过扩展方法来实现这一点:

您使用

bool isNumber = "-1990".IsNumber();

以下代码:

public static class NumberStringExtension
{

    public static bool IsNumber(this string value)
    {
        int i = 0;
        return int.TryParse(value, out i));
    }

}

You can achieve this by extension methods:

You use

bool isNumber = "-1990".IsNumber();

Here is code:

public static class NumberStringExtension
{

    public static bool IsNumber(this string value)
    {
        int i = 0;
        return int.TryParse(value, out i));
    }

}
锦上情书 2024-11-12 15:09:14

查看 int.TryParse 是否为您提供答案,并确保输入字符串中没有小数点。 (如果有人输入“1.”作为输入,这会给您带来误报。)

string input;
int ignored;

bool wholeNumber = int.TryParse(input, out ignored) && input.indexOf('.') == -1;

See if int.TryParse gives you an answer, and make sure there's not a decimal point in the input string. (This will give you a false positive if someone enters "1." as the input.)

string input;
int ignored;

bool wholeNumber = int.TryParse(input, out ignored) && input.indexOf('.') == -1;
薄情伤 2024-11-12 15:09:14

使用 Int.TryParse 或者制作自定义函数。

function bool IsNumber(object number)
{
  try
  {
    Convert.ToInt32(number.ToString());
    return true;
  }
  catch
  {
   return false;
  }
}

Use Int.TryParse Or make a custom function.

function bool IsNumber(object number)
{
  try
  {
    Convert.ToInt32(number.ToString());
    return true;
  }
  catch
  {
   return false;
  }
}
开始看清了 2024-11-12 15:09:14

根据定义,int 类型必须是整数。它必须是 double、float 或decimal 类型才能成为非整数。您始终可以尝试测试输入的类型。

或者也许输入是一个字符串?如果输入是字符串,您可以尝试搜索“.”或“,”字符(取决于文化)。或者您可以尝试解析为整数。

int types have to be whole numbers by definition. It would have to be type double, float, or decimal to be a non-whole number. You can always try testing the type of the input.

Or maybe the input is a string? If the input is a string, you could try searching for a '.' or ',' character (depending on the culture). Or you could try parsing as an integer.

千纸鹤带着心事 2024-11-12 15:09:14

由于每个 int 都是整数,因此这很简单:

public static bool IsWholeNumber(int i)
{
    return true;
}

Since every int is a whole number, this is easy:

public static bool IsWholeNumber(int i)
{
    return true;
}
葬花如无物 2024-11-12 15:09:14

如果您只想在 if 语句中验证它,您可以这样做

If ( val % 1 > 0)
  //not whole number

If you just want to validate it in the if statement you could just do

If ( val % 1 > 0)
  //not whole number
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文