欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

C# 多文本取数

using System;
using System.Text.RegularExpressions;//用于Split
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 WindowsFormsApp2
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
            label1.Text = "旧密码";
            label2.Text = "新密码";
            label3.Text = "多行文本";
            button1.Text = "检测";
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            string text = textBox3.Text;//调式可以看出是带\r\n的文本
            string[] lines = textBox3.Lines;//取每行内容
            string[] a = text.Split(new char[2] { '\r','\n' });//根据多字符多个分裂
            string[] b = Regex.Split(text, "\r\n", RegexOptions.IgnoreCase);//根据多字符单个分裂,但是要有using
            int maxnum = 0;//外面定义整型分数,并默认为0分
            string name1="";//外面定义字符串
            foreach (var item in lines)
            {
                string[] namenum = item.Split('=');//按等号分裂单行,分裂有是2位数组
                string name = namenum[0];
                int num = Convert.ToInt32(namenum[1]);
                if (num>maxnum)
                {
                    maxnum = num;
                    name1 = name;
                }
            }
            string result = name1 + Convert.ToString(maxnum);
            textBox2.Text = result;
            MessageBox.Show(string.Format("第一名是{0},分数是{1}", name1, maxnum));
        }

 

 

 

posted @ 2020-02-06 20:50  萧静默  阅读(240)  评论(0编辑  收藏  举报