日期:2014-05-17  浏览次数:20481 次

如何将下列字符串替换成我想要的字符串???(高手请进)
例如:单价=10
  str=月单价*月数+单价*天数

想要的结果为:str=月单价*月数+10*天数


也就是只是替换“单价”而不是把“月单价”也替换了



------解决方案--------------------
C# code

void Main()
{
    int 单价=10;
  string  str="月单价*月数+单价*天数";
  Console.WriteLine("替换前: "+str+"\t替换后: "+Regex.Replace(str,"([+\\-*/])单价","$1 "+单价));
  //替换前: 月单价*月数+单价*天数    替换后: 月单价*月数+ 10*天数

}

------解决方案--------------------
探讨
C# code

void Main()
{
int 单价=10;
string str="月单价*月数+单价*天数";
Console.WriteLine("替换前: "+str+"\t替换后: "+Regex.Replace(str,"([+\\-*/])单价","$1 "+单价));
//替换前: 月单价*月数+单价*天数 替换后: 月单价*月数+ ……