博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

判断字符串是否为全数字

Posted on 2011-06-16 22:53  Sky_KWolf  阅读(436)  评论(0编辑  收藏  举报
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 }