gpt4 book ai didi

vb.net - 仅检查 VB.NET 字符串中的数字

转载 作者:行者123 更新时间:2023-12-04 16:17:26 25 4
gpt4 key购买 nike

我想在将字符串附加到 StringBuilder 之前对其进行检查,以确保字符串中只有数字字符。有什么简单的方法可以做到这一点?

最佳答案

使用 Integer.TryParse() 如果字符串中只有数字,它将返回 true。 Int32 最大值是 2,147,483,647 所以如果你的值小于那么你的罚款。
http://msdn.microsoft.com/en-us/library/f02979c7.aspx

您还可以使用 Double.TryParse(),它的最大值为 1.7976931348623157E+308,但它允许使用小数点。

如果您想获得不是整数的值,您总是可以一次遍历一个字符串

string test = "1112003212g1232";
int result;
bool append=true;
for (int i = 0; i < test.Length-1; i++)
{
if(!Int32.TryParse(test.Substring(i,i+1),out result))
{
//Not an integer
append = false;
}
}

如果 append 保持为真,则该字符串是一个整数。可能是一种更灵活的方法,但这应该有效。

关于vb.net - 仅检查 VB.NET 字符串中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3374581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com