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;
}
}
}
實驗結(jié)果:



相關(guān)文章
WinForm中變Enter鍵為Tab鍵實現(xiàn)焦點轉(zhuǎn)移的方法
這篇文章主要介紹了WinForm中變Enter鍵為Tab鍵實現(xiàn)焦點轉(zhuǎn)移的方法,主要通過一個ControlTools類來實現(xiàn)該功能,需要的朋友可以參考下2014-08-08
DevExpress之ChartControl的SeriesTemplate實例
這篇文章主要介紹了DevExpress之ChartControl的SeriesTemplate用法實例,實現(xiàn)了餅狀Series百分比顯示的效果,具有一定的參考借鑒價值,需要的朋友可以參考下2014-10-10
C# Winform實現(xiàn)導(dǎo)出DataGridView當(dāng)前頁以及全部數(shù)據(jù)
基本上,所有的業(yè)務(wù)系統(tǒng)都會要求有導(dǎo)出的功能,所以這篇文章主要為大家介紹了如何利用Winform實現(xiàn)原生DataGridView的導(dǎo)出功能,需要的可以參考一下2023-07-07
C# 導(dǎo)出Excel的6種簡單方法實現(xiàn)
C# 導(dǎo)出 Excel 的6種簡單方法:數(shù)據(jù)表導(dǎo)出到 Excel,對象集合導(dǎo)出到 Excel,數(shù)據(jù)庫導(dǎo)出到 Excel,微軟網(wǎng)格控件導(dǎo)出到 Excel,數(shù)組導(dǎo)出到 Excel,CSV 導(dǎo)出到 Excel,你都會了嗎?需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

