替代方法C#未按预期 [英] Replace method c# not working as expected

查看:129
本文介绍了替代方法C#未按预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去取代双反斜线简单的一个,但它不工作。在这里,一个图像:

我仍然不知道为什么。我看到后如,并为他们工作。
建议?

更新

抱歉,我犯了一个错误,它的单反斜线只是我拼错了
在code替换为

 字符串clear_patch = path.Replace(@\\\\,\\);


解决方案

这是工作。 \\\\ 未逃脱,而在调试过程中检查。尝试将值打印到控制台窗口或网页。

*你似乎混淆编辑。

您路径实际上并不包含任何双斜杠 \\\\ ,它只是看起来是这样,因为在调试过程中查看字符串时单斜杠转义。考虑在<一起来阅读href=\"http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/what-character-escape-sequences-are-available.aspx\">Escape序列的。

如果你想逃避的斜杠那么就不要使用逐字字符串替换的文字如与string.replace(\\\\, - )这将连字符替换所有单个斜杠路径 - 虽然我不知道你为什么会想这样做?请注意,这仍然会显示路径具有 \\\\ 它斜杠,而调试期间检查。打印到控制台窗口将是:

<$c$c>C:-dev-vsprojects-VerificaionDeFirmas-VerificaionDeFirmas-verificaciondefirmas.txt

Im trying to replace double backslash for simple one but it does not work. Here an image:

I still wondering why. I saw post like this and it worked for them. Suggestions?

Update

Sorry i made a mistake, its a single backslashes just i spelled wrong the code to replace is

string clear_patch = path.Replace(@"\\","\");

解决方案

It is working. \\ is not escaped whilst inspecting during debugging. Try printing the value to a console window or the webpage.

Edit* You appear to be confused.

Your path doesn't actually contain any double slashes \\, it just looks that way because the single slashes are escaped when viewing the string during debugging. Consider reading up on Escape sequences.

If you want to escape single slashes then don't use a verbatim string literal in your replace e.g. string.Replace("\\", "-") which would replace all single slashes in the path with a hyphen - though I'm not sure why you would want to do this? Note that this will still show the path as having \\ slashes in it whilst inspecting during debugging. Printing to a console window would read:

C:-dev-vsprojects-VerificaionDeFirmas-VerificaionDeFirmas-verificaciondefirmas.txt

这篇关于替代方法C#未按预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆