Curious,
Not sure what you're doing wrong in your code but here is mine and it
works as expected. I'm using VS.NET 2005.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace RegEx
{
class Program
{
static void Main(string[] args)
{
string test = "Period Ending July 31, 2007";
string test2 = Regex.Replace(test, "Ending", "Beginning");
// Test2 is now "Period Beginning July 31, 2007"
}
}
}
Also not that your temp.Contains(...) is not necessary. If
Regex.Replace(...) find it, it will replace the string. No need to
check for it.
Jason Newell
Software Engineer
www.jasonnewell.net
Curious wrote:
I have another question about Regular Expression. If I use:
if (temp.Contains("Ending") == true)
{
temp =
System.Text.RegularExpressions.Regex.Replace(temp, "Ending",
"Beginning");
}
It seems that while "Ending" is replaced with "Beginning", it also
deletes the space after "Ending". For instance, "Period Ending July
31, 2007" is now "Period BeginningJuly 31, 2007" (Note: no space
betweem "Beginning" and "July" after Replace).
Any advice?