当前位置:网站首页>Search and replacement of C text file (WinForm)
Search and replacement of C text file (WinForm)
2022-04-23 07:54:00 【Dake mountain man】
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 = " We found :\r\n" + count.ToString() + "\r\n results .";
}
void DisplayReplaceCount(int count)
{
DisplaySearchCount(count);
lblReplaceResult.Text = " We found :\r\n" + count.ToString() + "\r\n results .";
}
private int CountNumber(string father, string son)
{
// Determine whether there is the same string
if (father.Contains(son))
{
// Use a string to store the whole replaced string
string strReplaced = father.Replace(son, "");
// The difference in the length of two strings / The unit length of the replaced string
return (father.Length - strReplaced.Length) / son.Length;
}
// No return 0 individual
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);
}
// write file
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);
}
}
}
}
版权声明
本文为[Dake mountain man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230625378262.html
边栏推荐
- Idea shortcut
- TA notes of Zhuang understand (VII) < Lambert + Phong + shadow + 3evcolor + Ao >
- Houdini > variable building roads, learning process notes
- .NET 5 的新功能 What‘s new in .NET 5
- Dropping Pixels for Adversarial Robustness
- NodeJS(一) 事件驱动编程
- Simplify exporting to SVG data files and all images in SVG folder
- NodeJS(六) 子进程操作
- 一些关于网络安全的好教程或笔记的链接,记录一下
- The problem of exporting excel form with wireframe and internal spacing of form by using web form
猜你喜欢
Towords Open World Object Detection
Export all SVG files in the specified path into pictures in PNG format (thumbnail or original size)
Houdini > rigid body, rigid body breaking RBD
对复杂字典Dictionary&lt;T1,T2&gt;排序问题
Mongodb 启动警告信息处理
向量到一个平面的投影向量
[unity VFX] Introduction notes of VFX special effects - spark production
【NLP笔记】CRF原理初探
Window10版MySQL设置远程访问权限后不起效果
Enterprise wechat login free jump self built application
随机推荐
企业微信免登录跳转自建应用
C SVG path parser of xamarin version
Post of experience in preparation for guarantee and research -- the 18th (2021) Central South planning department promoted the exemption to Zhejiang University Institute of Technology
Houdini>刚体, 刚体破碎RBD
Unity gets the resources that a file depends on
事件管理之一
一文了解系列,对web渗透的常见漏洞总结(持续更新)
Window10版MySQL设置远程访问权限后不起效果
Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
Export all SVG files in the specified path into pictures in PNG format (thumbnail or original size)
C# 读取注册表
Towords Open World Object Detection
NodeJS(一) 事件驱动编程
Nodejs (II) read files synchronously and asynchronously
Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)
取得所有点列表中的最大值GetMaxPoint
Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间
SampleCameraFilter
读取修改resource文件夹下的json文件
关于unity获取真实地理地图转3D化的相关链接