C#字符串替换不工作 [英] C# string replace does not work
问题描述
我试图用另一个另一个字符串替换字符串的一部分。为了更precise
我有 C:\\用户\\桌面\\项目\\ BIN \\调试
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
和我试图取代 \\ BIN \\调试
与 \\资源\\人民
我曾尝试以下内容:
A) path.Replace(@\\ BIN \\调试,@\\资源\\人\\ VisitingFaculty.txt);
B) path.Replace(\\\\ \\\\斌调试,\\\\ \\\\资源\\\\人VisitingFaculty.txt);
上述三个都不似乎工作,作为字符串保持不变,没有被替换。难道我做错了什么?
None of the above three seems to work, as the string remains the same and nothing is replaced. Am I doing something wrong?
推荐答案
的问题是,字符串是不可变的。该方法代替,串等不改变字符串本身。他们创造一个新的字符串和替换它。因此,对于上述code是正确的应该是
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");
或只是
path = path.Replace("\\bin\\Debug", "\\Resource\\People\\VisitingFaculty.txt");
如果不需要另一个变量
这篇关于C#字符串替换不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!