44

I am trying to replace a part of string with another another string. To be more precise I have C:\Users\Desktop\Project\bin\Debug

and I am trying to replace \bin\Debug with \Resources\People

I have tried the following:

  1. path.Replace(@"\bin\Debug", @"\Resource\People\VisitingFaculty.txt");

  2. path.Replace("\\bin\\Debug", "\\Resource\\People\\VisitingFaculty.txt");

None of the above two seems to work, as the string remains the same and nothing is replaced. Am I doing something wrong?

John Demetriou
  • 4,093
  • 6
  • 52
  • 88
  • 4
    "it doesn't work" is something you really shouldn't ever have in a question. What doesn't work? What does it do? What doesn't it do that it should? Are there errors, if so, what are they? – Servy Nov 07 '12 at 20:31
  • 2
    Doesn't work means does not do what it is supposed to do, replace the string with another string. This is a Q&A style question. I just found out the answer and created this Q&A style question – John Demetriou Nov 07 '12 at 20:35
  • 1
    While it's okay to self answer a question, the question will still be held to just the same standards. This question, if asked alone, would be a low quality question. Your answer doesn't (and can't) affect that. As I said, "it doesn't work" means nothing. You need to state both what you expect it to do, or want it to do, as well as what it actually does. In this case, "it doesn't work" means that `path` is not changed; it contains the original string. It doesn't error, it doesn't change the string to something other than the desired output, and the problem isn't conditional. – Servy Nov 07 '12 at 20:37
  • I believe you are right. I will edit it to be more correct and up to the standards for better understanding – John Demetriou Nov 07 '12 at 20:40
  • `path.Replace` has a return value, i.e. not `void`. That should have been your main clue as to how to use it. – rory.ap May 23 '16 at 23:00
  • @roryap I actually added this Q&A because a lot of people miss that, They forget that strings are immutable and expect it to change from within. I answered it myself because I already knew the answer. But a lot of newcomers to the language do not – John Demetriou Jun 06 '16 at 07:30
  • @Gholamali-Irani was changing a and b to a list really worth the edit effort? or was it just for the edit points? :) – John Demetriou Feb 09 '18 at 10:58
  • 1
    @JohnDemetriou, lists are better than a and b in the posts, however it is your question. You can rollback it. – Gholamali Irani Feb 09 '18 at 12:57
  • @Gholamali-Irani nah i'm good. It's just that 5 years later nobody thought it was bad :) – John Demetriou Feb 10 '18 at 09:16

3 Answers3

113

The problem is that strings are immutable. The methods replace, substring, etc. do not change the string itself. They create a new string and replace it. So for the above code to be correct, it should be

path1 = path.Replace("\\bin\\Debug", "\\Resource\\People\\VisitingFaculty.txt");

Or just

path = path.Replace("\\bin\\Debug", "\\Resource\\People\\VisitingFaculty.txt");

if another variable is not needed.

This answer is also a reminder that strings are immutable. Any change you make to them will in fact create a new string. So keep that in mind with everything that involves strings, including memory management. As stated in the documentation here.

String objects are immutable: they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Demetriou
  • 4,093
  • 6
  • 52
  • 88
  • 6
    Or just `path = path.Replace("\\bin\\Debug", "\\Resource\\People\\VisitingFaculty.txt");` if another variable is not needed. – kprobst Nov 07 '12 at 20:31
  • adding it right now :D I firstly used a different variable so it is shown that it is in fact a returning string but I will ad that part aswell – John Demetriou Nov 07 '12 at 20:32
  • 2
    Up-voted both the question and answer because this solved the problem I was experiencing. I do not believe the down-vote was fair based on the question, and a search of "string.replace C# not working" brought me here. – Steven C. Britton Jun 11 '14 at 05:13
  • @user1040975 I do not understand. You have an issue? Does your strReturn contain the string to search for? – John Demetriou Jul 19 '17 at 11:44
  • 2
    Thanks, I was beating my head not understanding why a Replace wasn't working until I saw this. I forgot to assign it to the same variable. – Caverman Jul 28 '17 at 15:15
  • @Caverman Yeah. This question is also a reminder that strings are immutable. Any change you make to them will in fact create a new string, So keep that in mind with everything (including memory management) – John Demetriou Jul 29 '17 at 10:15
  • @Peter Mortensen Did you really just edit my post to add 2 full stops? really? You think that was so necessary – John Demetriou Jul 28 '20 at 09:01
15

The path.Replace method actually returns a string. You should do the following:

path = path.Replace("firstString", "secondString");
PaulG
  • 6,920
  • 12
  • 54
  • 98
  • This was a Q&A style question. I answered it myself – John Demetriou Nov 07 '12 at 20:33
  • 6
    @macrian And that doesn't mean that other people cannot answer. If someone else can provide an answer that they feel is better than yours they have every right to provide an additional answer. That's part of the purpose of SO; everyone is capable of responding and voting results in the best answers (regardless of author) tend to float to the top. – Servy Nov 07 '12 at 20:40
  • You are right again. I simply thought that when it is selfansered it is closed for others (This is my first self answered question). I was wrong :D – John Demetriou Nov 07 '12 at 20:41
  • Ouch... good point. – MarceloBarbosa Oct 27 '16 at 13:39
8
String.Replace(string,string) returns string. 

So, save the new path in some string variable.

path = path.Replace("\\bin\\Debug", "\\Resource\\People\\VisitingFaculty.txt"); 
skjcyber
  • 5,759
  • 12
  • 40
  • 60