473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 1816
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Barnes | last post by:
Does anyone know of a good way to use the JavaScript string.replace() method in an ASP form? Here is the scenario: I have a form that cannot accept apostrophes. I want to use the replace() so...
0
by: Rick Brandt | last post by:
Here is a simplified example of what I am passing to a Java SAX parser within a servlet via an HTTPRequest... <?xml version="1.0"?> <Record> <ID>1092853685</ID> <Notes><!]></Notes> </Record>...
1
by: Thomas | last post by:
It looks like the String.replace doesn't work in IE6.1. Anyone else has the same problem. I am using newest service package of IE and Win2K. Thanks
6
by: vigi98 | last post by:
Hello all, Can someone confirme that this: var strURLpiece = "UK & Ireland"; strURLpiece.replace("&", "%26"); replaces all occurrence of the character & by %26 in strURLpiece, ie that...
6
by: G. | last post by:
This is an obvious bug in the String.Replace function: //load a XML string into a document XmlDocument doc = new XmlDocument(); doc.LoadXml("<test id='' />"); //Obtain the string...
6
by: Marty | last post by:
Hi, I would like to replace "\r\n" by "_" within a specific string. I tried : strMyString.Replace('\r', '_'); strMyString.Replace('\n', '_'); or...
9
by: Mark Szlazak | last post by:
I don't think this is "do-able" but thought I'd better check. Say I want to replace certain names in some source code as long as they are not properties (dot properties) of objects. I could use a...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
3
by: Alun | last post by:
I need to replace all new line characters in a string with a valid XHTML line break tag <br />. I'm trying to use the string.Replace method for this. Here's some example code: String...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.