比如 
string  st1 = "124"
string  st2 = "asdf";
string  st3 = "sdf13";
自动判断出 st1  可以转换成数字形式 int i1 = Convert.int32(st1);
           st2 和st3 无法转换成数字形式如果做出判断呢

解决方案 »

  1.   

    bool FoundMatch = false;
    try {
    FoundMatch = Regex.IsMatch(SubjectString, "^\\d+$");
    } catch (ArgumentException ex) {
    // Syntax error in the regular expression
    }
      

  2.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Text.RegularExpressions;namespace TestApp
    {
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button1;
    private System.ComponentModel.Container components = null; public Form1()
    {
    InitializeComponent();
    } protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
            private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(96, 80);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "";
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(16, 184);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(256, 23);
    this.label1.TabIndex = 1;
    this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(112, 128);
    this.button1.Name = "button1";
    this.button1.TabIndex = 2;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.textBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    bool FoundMatch = false;
    try 
    {
    FoundMatch = Regex.IsMatch(this.textBox1.Text, "^\\d+$");

    catch (ArgumentException ex) 
    {
    // Syntax error in the regular expression
    }
    this.label1.Text=FoundMatch?"能转化为整数":"不能转化为整数";
    }
    }
    }测试通过
      

  3.   

    Regex.IsMatch(SubjectString, "^\\d+$");
      

  4.   

    private const string REGEXP_IS_VALID_INT  = @"^\d{1,}$";  
    public static bool IsNumber(string str)
    {
    System.Text.RegularExpressions.Regex regex=new Regex(REGEXP_IS_VALID_INT);
              if(regex.IsMatch(str))
             return true;
    else
    return false;
    }