当前位置:网站首页>C# 文本文件的查找及替换(WinForm)
C# 文本文件的查找及替换(WinForm)
2022-04-23 06:26:00 【大可山人】
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.Threading.Tasks;
using System.Windows.Forms;
namespace SupperReplace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
openFileDialog1.AddExtension = true;
openFileDialog1.DefaultExt = "txt";
openFileDialog1.Filter = @"*.txt|*.txt";
}
private void btnSelectFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtBoxFile.Text = openFileDialog1.FileName;
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
string search = txtBoxSearch.Text.Trim();
if (!string.IsNullOrEmpty(search))
{
string fileName = txtBoxFile.Text.Trim();
using (TextReader tr = File.OpenText(fileName))
{
string content = tr.ReadToEnd();
int count = CountNumber(content, search);
DisplaySearchCount(count);
}
}
}
void DisplaySearchCount(int count)
{
lblSearchResult.Text = "共查找到:\r\n" + count.ToString() + "\r\n个结果。";
}
void DisplayReplaceCount(int count)
{
DisplaySearchCount(count);
lblReplaceResult.Text = "共查找到:\r\n" + count.ToString() + "\r\n个结果。";
}
private int CountNumber(string father, string son)
{
//判断是否有相同字符串
if (father.Contains(son))
{
//用一个字符串储存被替换后的整个字符串
string strReplaced = father.Replace(son, "");
//两个字符串的长度差值 / 被替换掉的字符串单位长度
return (father.Length - strReplaced.Length) / son.Length;
}
//没有返回0个
return 0;
}
private void btnReplace_Click(object sender, EventArgs e)
{
string search = txtBoxSearch.Text;
string replace = txtBoxReplace.Text;
if (!string.IsNullOrEmpty(search))
{
string fileName = txtBoxFile.Text.Trim();
string content, result;
using (TextReader tr = File.OpenText(fileName))
{
content = tr.ReadToEnd();
result = content.Replace(search, replace);
}
//写入文件
using (FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
Byte[] info = new UTF8Encoding(true).GetBytes(result);
fs.Write(info, 0, info.Length);
}
int count = CountNumber(content, search);
DisplayReplaceCount(count);
}
}
}
}
版权声明
本文为[大可山人]所创,转载请带上原文链接,感谢
https://blog.csdn.net/johnsuna/article/details/124332634
边栏推荐
猜你喜欢

Super classic & Programming Guide (red and blue book) - Reading Notes

SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)

SAP PI/PO功能运行状态监控检查

Mysql 索引

SAP RFC_CVI_EI_INBOUND_MAIN BP主数据创建示例(仅演示客户)

h5本地存储数据sessionStorage、localStorage

js之预解析

SAP DEBUG调试FOR IN、REDUCE等复杂的语句

Design optimization of MySQL database

keytool: command not found
随机推荐
SAP PI/PO Soap2Proxy 消费外部ws示例
MySQL storage engine
防抖和节流
Ogldev reading notes
BTree、B+Tree和HASH索引
js之什么是事件?事件三要素以及操作元素
Django uses MySQL database to solve error reporting
SAP pi / PO rfc2restful publishing RFC interface is a restful example (proxy indirect method)
Nacos/sentinel网关限流和分组 (代码)
Date object (JS built-in object)
Mysql 索引
Reflection on the systematic design of Android audio and video caching mechanism
AuthorizationServer(授权服务器的简单搭建)
SAP PI/PO rfc2RESTful 發布rfc接口為RESTful示例(Proxy間接法)
js之DOM学习三种创建元素的方式
SAP ECC连接SAP PI系统配置
js之节点操作,为什么要学习节点操作
Visualization Road (IX) detailed explanation of arrow class
1. View databases and tables
快排的练习