C# string replace does not work as it doesn't replace the value [duplicate]
Asked Answered
H

3

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?

Honorine answered 7/11, 2012 at 20:29 Comment(9)
"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?Courses
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 questionHonorine
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.Courses
I believe you are right. I will edit it to be more correct and up to the standards for better understandingHonorine
path.Replace has a return value, i.e. not void. That should have been your main clue as to how to use it.Polk
@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 notHonorine
@Gholamali-Irani was changing a and b to a list really worth the edit effort? or was it just for the edit points? :)Honorine
@JohnDemetriou, lists are better than a and b in the posts, however it is your question. You can rollback it.Lipsey
@Gholamali-Irani nah i'm good. It's just that 5 years later nobody thought it was bad :)Honorine
H
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.

Honorine answered 7/11, 2012 at 20:29 Comment(7)
Or just path = path.Replace("\\bin\\Debug", "\\Resource\\People\\VisitingFaculty.txt"); if another variable is not needed.Spermiogenesis
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 aswellHonorine
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.Ecdysiast
@user1040975 I do not understand. You have an issue? Does your strReturn contain the string to search for?Honorine
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.Rhododendron
@Rhododendron 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)Honorine
@Peter Mortensen Did you really just edit my post to add 2 full stops? really? You think that was so necessaryHonorine
S
15

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

path = path.Replace("firstString", "secondString");
Snooze answered 7/11, 2012 at 20:32 Comment(4)
This was a Q&A style question. I answered it myselfHonorine
@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.Courses
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 :DHonorine
Ouch... good point.Gaytan
D
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"); 
Decorator answered 7/11, 2012 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.