winform提取文本(txt)中想要的内容

时间:2021-07-02 16:23:21   收藏:0   阅读:0

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

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

//List<string> ZZZString = new List<string>();//定义一个集合,用于储存数据
//StringBuilder StringBuilder = new StringBuilder();

//创建一个Click事件,用于打开读取文件
private void button1_Click(object sender, System.EventArgs e)
{
//System.Windows.Forms.OpenFileDialog objOpenFile = new System.Windows.Forms.OpenFileDialog();
//objOpenFile.Filter = "文本文档|*.txt";

//if (objOpenFile.ShowDialog() == DialogResult.OK)
//{
// //this.txtFile.Text = objOpenFile.FileName;
// string[] ReadText = File.ReadAllLines(objOpenFile.FileName, Encoding.Default);


// foreach (string item in ReadText)
// {
// foreach(char number in item)
// {
// //Regex regex = new Regex(@"^\d{24}$");
// if (Convert.ToInt32(number) >= 48 && Convert.ToInt32(number) <= 57)
// //if (item.ToUpper().Contains(""))//搜索指定字符串
// {
// //ZZZString.Add(number);//搜索到的内容存入ZzzString集合
// //ZZZString.Append(number.ToString());
// StringBuilder.Append(number);
// //richTextBox1.Text += item.ToString() + "\r\n";//添加一个richTextBox
// //label1.Text = ZzzString.Count + "条";//添加一个label
// }
// }
// if (!"".Equals(StringBuilder.ToString())) {
// richTextBox1.Text += "6J" + StringBuilder + "\n";
// StringBuilder.Clear();
// }
// }
//}
}

private void richTextBox1_TextChanged(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog objOpenFile = new System.Windows.Forms.OpenFileDialog();
objOpenFile.Filter = "文本文档|*.txt";

if (objOpenFile.ShowDialog() == DialogResult.OK)
{
//this.txtFile.Text = objOpenFile.FileName;
string[] ReadText = File.ReadAllLines(objOpenFile.FileName, Encoding.Default);


int a = 0;string b = "";
foreach (string item in ReadText)
{
if (a == 1) {
string input_links = Regex.Replace(item," ", "");
richTextBox1.Text += "6J" + input_links + ":" + b + "\r\n";
a = 0;
}
if (item.ToUpper().Contains("6J")) {
a = 1;
}
if (item.ToUpper().Contains("(Z) A")) {
b = "ZA";
}
if (item.ToUpper().Contains("(Z) B")) {
b = "ZB";
}
if (item.ToUpper().Contains("(Z) C")) {
b = "ZC";
}
if (item.ToUpper().Contains("(Z) D")) {
b = "ZD";
}
}
System.IO.File.WriteAllText(@"C:\Users\LG\Desktop\提取的6J码.txt", richTextBox1.Text);
}
}
}
}

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!