题目:
界面:
这个题目其实用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就行,如果看到老师运行的为什么可以,那多半是因为老师的窗体设置的很大,不会出现状态条无法出现的情况。
有什么问题大家也可以后台问我,目前本人是大四的一枚“无业游民”,无论是考试、编程还是生活,都可以来咨询哦,只要时间正常。共勉!