473,806 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string "Replace" method doesn't seem to work

I have:

if (temp.Contains( "Account") == true)
{
temp.Replace("A ccount", "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 1841
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("A ccount", "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.Reg ularExpressions .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.Reg ularExpressions .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
8176
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 that the apostrophe is automatically replace with two '' . Reason being--SQL Server does not like apostrophes being sent to database. I've tried to do this on the server side in the SQL area of the ASP page by writing a function (with some great...
0
1357
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> I get the following error in the server's log output...
1
16350
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
56284
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 strURLpiece becomes "UK %26 Ireland" ? Thanks a lot.
6
22215
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 again...Converts my '' to ""...strange, but ok. String strXML = doc.OuterXml; //trying to convert back the single quotes to double quotes
6
29635
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 strMyString.Replace(System.Environment.NewLine, '_');
9
3794
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 regular expression like: rx = /(?:(\.)|\b)(?:name1|name2|name3)\b/g; map = ; map = "a"; map = "b";
10
18642
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 parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The problem is I don't know the character to replace, just must replace the character at a known...
3
2677
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 description = "This is a video description \n Hello World"; description = description.Replace("\n", "<br />"); Literal1.Text = description;
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10372
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.