RichTextBox怎么实现替换

coffce460 2011-02-26 10:04:55
替换指定位置的字符串,不是一次性全部替换,试了好多方法都不行,求助
...全文
390 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Erayyy 2011-02-27
  • 打赏
  • 举报
回复
richTextbox1.Text = richTextbox1.Text.Remove(p, textBox1.TextLength);
richTextbox1.Text = richTextbox1.Text.Insert(p, textBox2.Text);
前面+richTextbox1.Text =
coffce460 2011-02-27
  • 打赏
  • 举报
回复
int p = richTextbox1.Text.IndexOf(textBox1.Text);和int p = richTextBox1.Find(textBox1.Text);
yalan 2011-02-26
  • 打赏
  • 举报
回复
p是光标位置?
你怎么得到的p?


行值=(1+richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart)).ToString();

列值=(1+richTextBox1.SelectionStart-(richTextBox1.GetFirstCharIndexFromLine(1+richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart)-1))).ToString();
coffce460 2011-02-26
  • 打赏
  • 举报
回复
是替换指定位置的字符串,问题解决了,是我刚才傻了
不过还是不明白一点

//p为光标位置,textBox1是查找框,textBox2是替换框
richTextbox1.Text.Remove(p, textBox1.TextLength);
richTextbox1.Text.Insert(p, textBox2.Text);
这个代码运行没反应

string re = richTextbox1.Text.Substring(p, textBox1.TextLength);
richTextbox1.Text.Substring(p, textBox1.TextLength).Replace(re,textBox2.Text);
这个也没反应,这是为什么呢

yalan 2011-02-26
  • 打赏
  • 举报
回复
[Quote=引用楼主 coffce460 的回复:]
替换指定位置的字符串,不是一次性全部替换,试了好多方法都不行,求助
[/Quote]

这个容易,不过我不知道你想怎么替换,你想指定替换第几行的某个文字还是怎么?
再说详细点你的要求把
wuyq11 2011-02-26
  • 打赏
  • 举报
回复
string key = Regex.Escape(textBox1.Text);
Regex reg = new Regex("");
MatchCollection mc = reg.Matches(str);
foreach (Match m in mc)
{
richTextBox1.SelectionStart = m.Index;
richTextBox1.SelectionLength = m.Value.Length;
}
查找数据替换
口圭Y口塞 2011-02-26
  • 打赏
  • 举报
回复
用正则表达式可以

还有 先搜索索引,然后选择搜索到的字符串,在对设置SelectedText差不多也行吧

110,572

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧