现在的位置: 首页 > 综合 > 正文

判断字符串是否为全数字

2011年10月29日 ⁄ 综合 ⁄ 共 627字 ⁄ 字号 评论关闭
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace test
7 {
8 class Program
9 {
10 // 检查字符串是否全为数字
11
12 public bool IsNum(string Str)
13 {
14 bool blResult = true;
15 if (Str == "")
16 blResult = false;
17 else
18 {
19 foreach (char Char in Str)
20 {
21 if (!Char.IsNumber(Char))
22 {
23 blResult = false;
24 break;
25 }
26 }
27 if (blResult)
28 if (int.Parse(Str) == 0)
29 blResult = false;
30 }
31 return blResult;
32 }
33
34 static void Main(string[] args)
35 {
36 Program p = new Program();
37 Console.WriteLine("请输入字符串");
38 string s = Console.ReadLine();
39 if (p.IsNum(s))
40 {
41 Console.WriteLine("全为数字");
42 }
43 else
44 {
45 Console.WriteLine("不是全为数字");
46 }
47 Console.ReadLine();
48
49 }
50 }
51 }

抱歉!评论已关闭.