C#中怎样从指定字符串中查找并替换字符串?
时间:2023-09-30 14:26:24
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
#region
#endregion
namespace Find
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string str = "";
richTextBox1.Text = str;
}
int start = 0;
int count = 0;
/// <summary>
/// 查找字符串
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void find_Click(object sender, EventArgs e)
{
string str1;
str1 = txt_find.Text;
if (start >= richTextBox1.Text.Length)
{
MessageBox.Show("以查找到尾部");
start = 0;
}
else
{
start = richTextBox1.Find(str1, start, RichTextBoxFinds.MatchCase);
if (start == -1)
{
if (count == 0)
{
MessageBox.Show("没有该字符!");
}
else
{
MessageBox.Show("以查找到尾部!");
start = 0;
}
}
else
{
start = start + str1.Length;
richTextBox1.Focus();
}
}
}
/// <summary>
/// 替换字符串
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void replace_Click(object sender, EventArgs e)
{
richTextBox1.Text = richTextBox1.Text.Replace(txt_find.Text, txt_replace.Text);
}
/// <summary>
/// 输入查找的字符串
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_find_TextChanged(object sender, EventArgs e)
{
string str1;
str1 = txt_find.Text;
start = 0;
count = 0;
}
}
}
实验结果:
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中用内存映射处理大文件的实现代码
android使用DataBinding来设置空状态
![](https://img.aspxhome.com/file/2023/4/88344_0s.gif)
总结Java的Struts框架的异常处理方法
![](https://img.aspxhome.com/file/2023/1/108431_0s.jpg)
Struts2配置文件中使用通配符的方法(三种形式)
android中开启actionbar的两种方法
Android可自定义神奇动效的卡片切换视图实例
![](https://img.aspxhome.com/file/2023/6/97846_0s.gif)
Android 两个Fragment之间传递数据实例详解
SpringBoot实现MapperScan添加动态配置(占位符)
Android抓取CSDN首页极客头条内容完整实例
![](https://img.aspxhome.com/file/2023/8/120068_0s.png)
mvc开启gzip压缩示例分享
Mybatis中的like模糊查询功能
Struts2中validate数据校验的两种方法详解附Struts2常用校验器
![](https://img.aspxhome.com/file/2023/4/96604_0s.png)
c#中设置快捷键
c# 编写一个轻量级的异步写日志的实用工具类(LogAsyncWriter)
![](https://img.aspxhome.com/file/2023/3/98223_0s.png)
C#面向对象编程之猜拳游戏实现方法
WinForm实现窗体最大化并遮盖任务栏的方法
C#使用Log4.net记录日志文件
![](https://img.aspxhome.com/file/2023/7/106447_0s.png)
Android集成微信支付功能
OpenCV+Qt实现图像处理操作
![](https://img.aspxhome.com/file/2023/3/135083_0s.jpg)