如何判断一个字符串是否是正整型???

wxylvmnn 2008-01-30 11:21:13
如何判断一个字符串是否是正整型???

不要使用try catch
...全文
804 38 打赏 收藏 转发到动态 举报
写回复
用AI写文章
38 条回复
切换为时间正序
请发表友善的回复…
发表回复
U2008 2008-02-18
  • 打赏
  • 举报
回复
...
北京的雾霾天 2008-01-31
  • 打赏
  • 举报
回复
所以我觉得像楼主这样的问题这样判断会更好:

int tmp=-1;
if (int.TryParse(str, out tmp) && tmp>0)
{
//str字符串表示的是一个正整数
}
else
{
//str表示的是非整数或是小于0的整数
}
北京的雾霾天 2008-01-31
  • 打赏
  • 举报
回复
是最大可以输入9个9,因为10个9就大于2147483647了。

但是你想1999999999是不是大于999999999,你输入试试看,2147483647是不是大于999999999,你输入试试看可以不。
cime63 2008-01-31
  • 打赏
  • 举报
回复
正则是简单有效的方法.
地下室小红叔 2008-01-31
  • 打赏
  • 举报
回复
1楼判断的是字符串是否为数字 考虑到书写规范 需要改改
        /// <summary>
/// 判断字符串是否为数字
/// </summary>
/// <param name="str">要判断的字符串对象</param>
/// <returns></returns>
public static bool IsNumber(string str)
{
return Regex.IsMatch(str, @"^\d+$");
}

/// <summary>
/// 判断字符串是否为正整数
/// </summary>
/// <param name="str">要判断的字符串对象</param>
/// <returns></returns>
public static bool IsInt(string str)
{
return Regex.IsMatch(str, @"^(0|([1-9]\d*))$");
}


当然 考虑到数值范围 如int32 int64等 还需要改正
wxylvmnn 2008-01-31
  • 打赏
  • 举报
回复
谢谢大家的热心帮忙。

其实这个问题不难,也有很多的解决方案。

这是我的最后解决方案。
"^([1-9]{1}[0-9]{0," & maxLen - 1 & "}|0)?$"

回答hbxtlhx的问题。

SQL SERVER的表的字段被定义成int时,最大可以输入9个9,不信你试下。

wdzr_826 2008-01-31
  • 打赏
  • 举报
回复
int result=0; int.TryParse(yourstring, out result); if(result>0) { ... }
-----------
这个方法最好的。
r_swordsman 2008-01-31
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace TempTestA1
{
class Class7
{
public static void Main()
{
int r = 0;
// 只允许前导空白、结尾空白、千分位符
Console.WriteLine("下列返回 true");
Console.WriteLine(int.TryParse("123456", NumberStyles.AllowLeadingWhite | NumberStyles.AllowThousands | NumberStyles.AllowTrailingWhite, null, out r));
Console.WriteLine(int.TryParse("123,456", NumberStyles.AllowLeadingWhite | NumberStyles.AllowThousands | NumberStyles.AllowTrailingWhite, null, out r));
Console.WriteLine(int.TryParse(" \r\n 123,456 \r\n ", NumberStyles.AllowLeadingWhite | NumberStyles.AllowThousands | NumberStyles.AllowTrailingWhite, null, out r));
Console.WriteLine(int.TryParse("0", NumberStyles.AllowLeadingWhite | NumberStyles.AllowThousands | NumberStyles.AllowTrailingWhite, null, out r));

// 正负符号、16 进制、小数点等不允许
Console.WriteLine("下列返回 false");
Console.WriteLine(int.TryParse("+123456", NumberStyles.AllowLeadingWhite | NumberStyles.AllowThousands | NumberStyles.AllowTrailingWhite, null, out r));
Console.WriteLine(int.TryParse("-0", NumberStyles.AllowLeadingWhite | NumberStyles.AllowThousands | NumberStyles.AllowTrailingWhite, null, out r));
Console.WriteLine(int.TryParse("123.456", NumberStyles.AllowLeadingWhite | NumberStyles.AllowThousands | NumberStyles.AllowTrailingWhite, null, out r));
Console.WriteLine(int.TryParse("0xb345", NumberStyles.AllowLeadingWhite | NumberStyles.AllowThousands | NumberStyles.AllowTrailingWhite, null, out r));
}

}
}

viena 2008-01-30
  • 打赏
  • 举报
回复
正则表达式改为这样,加了+号也不能说不是整数了
@"^\+?\d+$
god_is_man 2008-01-30
  • 打赏
  • 举报
回复
楼上正解
manonroad 2008-01-30
  • 打赏
  • 举报
回复
我上面写的有个Bug,重贴一次。


public static bool IsPositiveInt(string strInt)
{
char[] chaStr = strInt.ToCharArray();
bool getFirst = false;
foreach (char c in chaStr)
{
if (!getFirst)
{
if (c.Equals('+') || char.IsDigit(c))
{
getFirst = true;
continue;
}
else
{
return false;
}
}
else
{
if (!char.IsDigit(c))
{
return false;
}
}
}
return true;
}
viena 2008-01-30
  • 打赏
  • 举报
回复
用正则表达式,1楼正解~
ipqxiang 2008-01-30
  • 打赏
  • 举报
回复
int.TryParse
manonroad 2008-01-30
  • 打赏
  • 举报
回复
你只有自己写了,试试看下面的代码。再加点分吧。:)

public bool IsPositiveInt(string strInt)
{
char[] chaStr = strInt.ToCharArray();
bool getFirst = false;
foreach (char c in chaStr)
{
if (!getFirst)
{
if (!c.Equals('+'))
{
return false;
}
getFirst = true;
}
else
{
if (!char.IsDigit(c))
{
return false;
}
}
}
return true;
}
showrock 2008-01-30
  • 打赏
  • 举报
回复
正则啦

int result=0;
int.TryParse(yourstring, out result);
if(result>0)
{
...
}

这方法第一回见呢,不错哦
xiaoqhuang 2008-01-30
  • 打赏
  • 举报
回复
用正则表达式。
hrb133yqq 2008-01-30
  • 打赏
  • 举报
回复
觉得还是循环一下来的直接

bool isPositveInt(string s)
{
if(s.Trim().IsNullOrEmpty())
{
return false;
}

bool b = true;
foreach(char c in s)
{
if(c>'9'||c<'0')
{
b = false;
}
}

return b;
}

没有测试
wxylvmnn 2008-01-30
  • 打赏
  • 举报
回复
我的兄弟姐妹们啊,int.TryParse不是还得判断得大于0麽?

int.TryParse("-99999999",0)仍然是true.

小小暴徒 2008-01-30
  • 打赏
  • 举报
回复
int.TryParse
Snowdust 2008-01-30
  • 打赏
  • 举报
回复

int i = 0;
string str = "123";
if (!int.TryParse(str, out i)
{
//不是整型
}
else
{
//是整型
}
加载更多回复(18)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧