473,466 Members | 4,869 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

string.replace acts unexpectedly in this case

string foo = "v:\\bar.txt";
foo.Replace(@"\\",@"\");
if(foo == "v:\\bar.txt")
MessageBox.Show("Why the heck didn't Replace work? I'm confusied!");
Nov 22 '05 #1
3 1089
> string foo = "v:\\bar.txt";
foo.Replace(@"\\",@"\");
if(foo == "v:\\bar.txt")
MessageBox.Show("Why the heck didn't Replace work? I'm confusied!");


Could you please explain what you're expecting the code to replace?

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 22 '05 #2
string foo does not have the @ in front of it. This means that the \\
is actuall a single \
v:\bar.txt

the call to foo.Replace looks for \\ but doesn't find it, so nothing is
changed.

If you want foo to have \\ then put an @ in front of it or write it as
\\\\
Rob Vretenar [imason inc.]

Nov 22 '05 #3
William Sullivan <Wi*************@discussions.microsoft.com> wrote:
string foo = "v:\\bar.txt";
is equivalent to:

string foo = @"v:\bar.txt";
foo.Replace(@"\\",@"\");
has no effect on 'foo', due to String being immutable.
if(foo == "v:\\bar.txt")
is equivalent to:

if(foo == @"v:\bar.txt")
[...]


This should do what you're after:

const string StringWithCuriousDoubleSlash = @"v:\\bar.txt";

string foo = StringWithCuriousDoubleSlash;
foo = foo.Replace(@"\\",@"\");

if (foo != StringWithCuriousDoubleSlash
MessageBox.Show("Success.");
Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William Sullivan | last post by:
string foo = "v:\\bar.txt"; foo.Replace(@"\\",@"\"); if(foo == "v:\\bar.txt") MessageBox.Show("Why the heck didn't Replace work? I'm confusied!");
1
by: William Sullivan | last post by:
Grrrr... I couldn't edit my previous post. Lemme short circuit the obvious reply by fixing my code. string foo = @"v:\\bar.txt"; foo.Replace(@"\\",@"\"); if(foo == @"v:\\bar.txt)...
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
6
by: archana | last post by:
Hi all, I am facing some wierd problem in quick watch at a time of debugging my application. I have one variable of type string, say suppose i have code as below: string FileName;...
10
by: asimrafi | last post by:
Hi! i m new to this forum and need some help in ASP how can i validate a string so that it have only the characters of ASCII 32 to 91 and 93 to 122 ? Also if the string contains the comma (,)...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
6
by: fnoppie | last post by:
Hi, I am near to desperation as I have a million things to get a solution for my problem. I have to post a multipart message to a url that consists of a xml file and an binary file (pdf)....
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
3
by: kronus | last post by:
I'm receiving an xml file that has a child called modified and it represents a date value in the form of a string -- Nov 14, 2008 -- and in my app, I have items associated with each object and I'm...
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...
1
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...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...

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.