string "Replace" method doesn't seem to work

  • Thread starter Thread starter Curious
  • Start date Start date
C

Curious

I have:

if (temp.Contains("Account") == true)
{
temp.Replace("Account", "Client");

}


The "Account" is not replaced by "Client" after this operation. I used
debugger and see temp remains the same after "Replace".

Any advice? Thanks!
 
Thanks!

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?
 
Curious said:
Thanks!

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?

Use string.Replace instead. There is no reason to use Regex.Replace when
you don't have a regular expression.

If you _do_ have a regular expression, but have replaced it in the
example, show the actual code that you are using. It's very hard to find
errors in code that you can't see.
 
Thanks - Either string Replace or Regex Replace works. The problem is
that it doesn't show the correct string in the PDF file...
 
Curious said:
Thanks - Either string Replace or Regex Replace works. The problem is
that it doesn't show the correct string in the PDF file...

Then you have to examine what the data looks like really. Either Replace
method doesn't remove spaces by itself, so it's not the replacement
method that is the problem, but the data that you use it on.

Perhaps the phrase is divided into two text areas, so that changing the
text "Period Ending" doesn't move the date as it's in a separate area.
 
Back
Top