有这样一个功能需求,有一个字符串,例如:“这是要处理的字符串这是要处理的字符串这是要处理的字符串”,在这个字符串中“这是”共出现了3次,如何只把第一个“这是”替换成“那是”,其它内容不变。
如何用C#实现呢?求帮助!
如何用C#实现呢?求帮助!
解决方案 »
- the instancestore could not be initialized
- 求:C# Kodak控件 imgedit1图象高效旋转指定角度?
- 操作打印机任务,200分求代码(第二贴)!
- streamreader不能读取中文
- 请问Timer控件怎么使用? timer1.Enabled =true;报错
- dateTimePicker取值查询问题
- 研究一下个性台历在线制作网站的技术构成,有兴趣的朋友帮助一起看看
- 问题A:鼠标点哪个文本框,就切换到相对应的按钮的焦点 问题B:点击鼠标切换层内容的JS代码
- OleDB连接中的问题
- 请问。。。
- 多个TableAdapter 使用事务
- 杀进程的问题!!!!求教各位大神
string s = "这是要处理的字符串这是要处理的字符串这是要处理的字符串";
s = Regex.Replace(s, "这是", new MatchEvaluator(m => {
if (isReplaced) return m.Value;
isReplace = true;
return "那是";
}));
string str="这是要处理的字符串这是要处理的字符串这是要处理的字符串”;
int index = str.IndexOf("这是");
str=str.Remove(index,2).Insert(index,"那是"));
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace StringReplace
{
class Program
{
static void Main(string[] args)
{
string txt = "这是要处理的字符串这是要处理的字符串这是要处理的字符串";
//声明一个词语
string str = "这是";
int index = txt.IndexOf(str);
int indexLast = txt.LastIndexOf(str);
while (true)
{ txt=txt.Remove(index,2).Insert(index,"那是");
index = txt.IndexOf(str); if (index==indexLast)
{
break;
}
}
txt = txt.Remove(index, 2).Insert(index, "那是");
index = txt.IndexOf(str);
Console.WriteLine(txt);
Console.ReadKey(); }
}
}