470,815 Members | 1,308 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

string "Replace" method doesn't seem to work

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!

Aug 28 '07 #1
5 1695
Curious,
Replace() returns the modified string so you'll need:

temp = temp.Replace();

Jason Newell
Software Engineer
www.jasonnewell.net

Curious wrote:
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!
Aug 28 '07 #2
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?

Aug 28 '07 #3
Curious wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 28 '07 #4
Thanks - Either string Replace or Regex Replace works. The problem is
that it doesn't show the correct string in the PDF file...

Aug 28 '07 #5
Curious wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 29 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Rick Brandt | last post: by
6 posts views Thread by vigi98 | last post: by
6 posts views Thread by Marty | last post: by
9 posts views Thread by Mark Szlazak | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.