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;
}
}
}
實(shí)驗(yàn)結(jié)果:
相關(guān)文章
C#編程實(shí)現(xiàn)對(duì)象與JSON串互相轉(zhuǎn)換實(shí)例分析
這篇文章主要介紹了C#編程實(shí)現(xiàn)對(duì)象與JSON串互相轉(zhuǎn)換的方法,結(jié)合實(shí)例分析了在DoNet2.0與Donet3.5環(huán)境下實(shí)現(xiàn)對(duì)象與JSON轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-11-11淺談C#2.0泛型中的變化:default關(guān)鍵字
下面就詳細(xì)的說明一下。之所以會(huì)用到default關(guān)鍵字,是因?yàn)樾枰诓恢李愋蛥?shù)為值類型還是引用類型的情況下,為對(duì)象實(shí)例賦初值2013-09-09C#中使用ArrayPool和MemoryPool實(shí)例
對(duì)資源的可復(fù)用是提升應(yīng)用程序性能的一個(gè)非常重要的手段,比如本篇要分享的 ArrayPool 和 MemoryPool,它們就有效的減少了內(nèi)存使用和對(duì)GC的壓力,從而提升應(yīng)用程序性能。感興趣的可以了解一下2021-05-05C#實(shí)現(xiàn)合并多個(gè)word文檔的方法
這篇文章主要介紹了C#實(shí)現(xiàn)合并多個(gè)word文檔的方法,是C#針對(duì)Word文檔操作的一個(gè)非常重要的技巧,需要的朋友可以參考下2014-09-09C#以太網(wǎng)Sockets服務(wù)器設(shè)計(jì)實(shí)現(xiàn)
本文主要介紹了C#以太網(wǎng)Sockets服務(wù)器設(shè)計(jì)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02C#儀器數(shù)據(jù)文件解析Excel文件的方法淺析(xls、xlsx)
這篇文章主要給大家介紹了關(guān)于C#儀器數(shù)據(jù)文件如何解析Excel文件的方法,包括解析xls、xlsx兩種格式,文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10