C# .net窗体实战2:字符串提取

题目:

界面:

这个题目其实用char在string进行遍历能够很快的进行字符串的提取,有一年的题目是提取英文字符,并且还需要进行a-z、A-Z、1-9的排序,大家也可以思考一下怎么进行数字和字符的排序。

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            // inputString = textBox1.Text.Trim();//Trim是移除当前textBox1中的空格
            string inputString = textBox1.Text;

            if (string.IsNullOrEmpty(inputString))//IsNullOrEmpty判断是否为空
            {
                MessageBox.Show("输入字符串不能为空!");
                return;
            }

            //设置三个字符串接收分离的字符
            string digits = "";
            string letters = "";
            string others = "";

            foreach (char c in inputString)
            {
                if (char.IsDigit(c))//与 c >= 0x0030 && c <= 0x0039同理
                {
                    digits += c;
                }
                else if ((c >= 0x0041 && c <= 0x005A) | (c >= 0x0061 && c <= 0x007A))
                {//与 character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z'同理
                    letters += c;
                }
                else
                {
                    others += c;
                }
            }
            textBox2.Text = digits;
            textBox3.Text = letters;
            textBox4.Text = others;

        }
    }
}

这个题目并不是很难,我觉得这种方法是比较方便并且好理解,事实上还有其他更加简单的方法大家可以去查阅一下。

除了在代码中实现操作控件的一些功能,大家也要注意学习窗体、控件中的基本属性,比如说采用状态条的时候,运行之后找不到X、Y,只需要在窗体中的AutoScaleMode设置一下不要Font就行,如果看到老师运行的为什么可以,那多半是因为老师的窗体设置的很大,不会出现状态条无法出现的情况。

有什么问题大家也可以后台问我,目前本人是大四的一枚“无业游民”,无论是考试、编程还是生活,都可以来咨询哦,只要时间正常。共勉!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值