C#中怎样从指定字符串中查找并替换字符串?
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;
}
}
}
实验结果:



相关文章
DevExpress之ChartControl的SeriesTemplate实例
这篇文章主要介绍了DevExpress之ChartControl的SeriesTemplate用法实例,实现了饼状Series百分比显示的效果,具有一定的参考借鉴价值,需要的朋友可以参考下2014-10-10
C# Winform实现导出DataGridView当前页以及全部数据
基本上,所有的业务系统都会要求有导出的功能,所以这篇文章主要为大家介绍了如何利用Winform实现原生DataGridView的导出功能,需要的可以参考一下2023-07-07


最新评论