大家一起来编程第十二期

题目:

                                   任意输入一串数字,找出其中某一个数字在字串中的位置例如:
                                   输入:4,8,12,21,5,65,23,7,128,90,15,87,2,56
                                  输入想查找的数字:90
                                  程序就可以得出:在第10位或a[9](可以自己定义)

我的回答:

我是采用正则表达式完成对输入字符串的分解的。



using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;            //必须引用这一条

namespace ZiFuChuan
{
        /// <summary>
        /// Description of MainForm.
        /// </summary>
        public class MainForm : System.Windows.Forms.Form
        {
                private System.Windows.Forms.Button button1;
                private System.Windows.Forms.TextBox textBox1;
                private System.Windows.Forms.TextBox textBox2;
                public MainForm()
                {
                        //
                        // The InitializeComponent() call is required for Windows Forms designer support.
                        //
                        InitializeComponent();
                       
                        //
                        // TODO: Add constructor code after the InitializeComponent() call.
                        //
                }
               
                [STAThread]
                public static void Main(string[] args)
                {
                        Application.Run(new MainForm());
                }
               
                #region Windows Forms Designer generated code
                /// <summary>
                /// This method is required for Windows Forms designer support.
                /// Do not change the method contents inside the source code editor. The Forms designer might
                /// not be able to load this method if it was changed manually.
                /// </summary>
                private void InitializeComponent() {
                        this.textBox2 = new System.Windows.Forms.TextBox();
                        this.textBox1 = new System.Windows.Forms.TextBox();
                        this.button1 = new System.Windows.Forms.Button();
                        this.SuspendLayout();
                        //
                        // textBox2
                        //
                        this.textBox2.Location = new System.Drawing.Point(88, 104);
                        this.textBox2.Name = "textBox2";
                        this.textBox2.Size = new System.Drawing.Size(152, 21);
                        this.textBox2.TabIndex = 1;
                        this.textBox2.Text = "90";
                        //
                        // textBox1
                        //
                        this.textBox1.Location = new System.Drawing.Point(88, 56);
                        this.textBox1.Name = "textBox1";
                        this.textBox1.Size = new System.Drawing.Size(272, 21);
                        this.textBox1.TabIndex = 0;
                        this.textBox1.Text = "4,8,12,21,5,65,23,7,128,90,15,87,2,56";
                        //
                        // button1
                        //
                        this.button1.Location = new System.Drawing.Point(88, 152);
                        this.button1.Name = "button1";
                        this.button1.Size = new System.Drawing.Size(88, 24);
                        this.button1.TabIndex = 2;
                        this.button1.Text = "Go";
                        this.button1.Click += new System.EventHandler(this.Button1Click);
                        //
                        // MainForm
                        //
                        this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                        this.ClientSize = new System.Drawing.Size(432, 237);
                        this.Controls.Add(this.button1);
                        this.Controls.Add(this.textBox2);
                        this.Controls.Add(this.textBox1);
                        this.Name = "MainForm";
                        this.Text = "MainForm";
                        this.ResumeLayout(false);
                }
                #endregion
                void Button1Click(object sender, System.EventArgs e)
                {
                        string s = @"[0-9]*[0-9]";         //正则表达式。表示以任意符号分割开的数字字符串
                        int i=0;
                        bool b = false;
                        Regex r = new Regex(s, RegexOptions.None) ;

                        foreach( Match m in r.Matches(textBox1.Text))      //与给定的字符串匹配            
                        {
                                s=m.ToString();
                                i++;
                                if (s==textBox2.Text)                   //如果相同
                                {                          
                                        MessageBox.Show (textBox2.Text+
                                                         " is the No."+i.ToString()
                                                         +" number.");
                                        b=true;
                                        break;
                                }
                        }
                        if (b==false)
                                        MessageBox.Show("not found.");
                }
               
        }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值