手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#中怎样从指定字符串中查找并替换字符串?
C#中怎样从指定字符串中查找并替换字符串?
摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel...

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;

}

}

}

实验结果:

C#中怎样从指定字符串中查找并替换字符串?1

C#中怎样从指定字符串中查找并替换字符串?2

C#中怎样从指定字符串中查找并替换字符串?3

【C#中怎样从指定字符串中查找并替换字符串?】相关文章:

C# 操作符之二 算数操作符

C# char类型字符转换大小写的实现代码

获取字符串中的汉字拼音首字母

c#对字符串操作的技巧小结

C#中怎么将一个List转换为只读的

c#中判断字符串是不是数字或字母的方法

C#运算符之与,或,异或及移位运算小结

c#实现16进制和字符串之间转换的代码

C#中使用split分割字符串的几种方法小结

C# 字符串多行显示/文本换行以textbox为例讲解

精品推荐
分类导航