C#逐字字符串文字不起作用.反斜杠总是很奇怪 [英] C# verbatim string literal not working. Very Strange backslash always double

查看:175
本文介绍了C#逐字字符串文字不起作用.反斜杠总是很奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了很长时间,以弄清楚发生了什么,但是除了我以外的任何人都找不到这个问题的任何地方.

I've tried for quite a long time to figure out whats going on but I've not found anything anywhere that someone besides me has ran into this issue.

我只是试图将路径硬编码为字符串.简单的东西.好吧,出于某些原因

I'm simply trying to hard code a path into a string. Easy stuff. Well for some reason

string fullPathSourceFile = @"c:\SQLSOURCE.txt";

正在评估c:\\SQLSOURCE.txt

我已经尝试了所有方法,将其评估为一个反斜杠,删除了双引号,但它不起作用.我什至尝试了Replace(@"\\", @"\"),它没有任何影响.任何人都知道我的代码发生了什么事情,当应评估一个单反斜杠时会强制使用双反斜杠吗?这真让我发疯,这太容易了,却使我很沮丧.

I've tried everything to evaluated it to a single backslash remove the double quotes and it wont work. I even tried Replace(@"\\", @"\") and it has no affect. Anyone have any idea what's going on with my code that would force a double backslash when a single one should be evaluated? This is driving me nuts and it's so damn easy yet causing me a lot of frustration.

然后我在下面使用字符串变量:

I'm then using the string variable below:

                using (StreamReader reader = new StreamReader(fullPathSourceFile))
                {
                    string line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        sqlDBsource = line.ToString();
                    }

                    reader.Close();
                }

感谢大家的投入,这有助于我弄清楚我做错了什么.在Visual Studio(令人困惑)中,当您在调试器中查看字符串的值时,它将为您添加转义符,因此监视窗口中的双反斜杠或变量值弹出窗口是正常的,并不意味着实际上有两个反斜杠.当您将鼠标悬停在变量上或在监视窗口中观看变量时,单击工具提示/窗格右侧的放大镜图标,这将向您显示未转义的字符串,该字符串将被打印到控制台.显示实际结果的另一种方法是:Console.WriteLine(the_problem_string);我在代码中遇到的问题不在本文讨论范围之内,但是从Visual Studio中看到的结果混乱使我相信,如果不是,则字符串是问题的根源.

Thanks to everyone for their input which helped my figure out what I was doing wrong. In Visual Studio (which is confusing) when you look at the value of a string in the debugger, it puts escapes in for you, so a double-backslash in a watch window or variable value popup is normal and does not mean there are actually two backslashes. When you mouse-over the variable or watch it in the watch window, click the magnifying glass icon at the right hand side of the tooltip/pane, this will show you the unescaped string at it would be printed to the console. Another way to display the actual results is: Console.WriteLine(the_problem_string); The issue I was having with the code is outside the scope of the post but the confusion of the results I was seeing from Visual Studio lead me to believe the string was the source of the problem when it wasn't.

推荐答案

这很奇怪.因此,我按照注释中的建议删除了逐字记录,当我在字符串中使用双反斜杠时,它就起作用了.由于某种原因,该代码不喜欢逐字字符串,并且错误地翻译了反斜​​杠.这解决了问题.如果有人遇到这种情况,您可能需要使用逐字/非普通字符串,因为在某些情况下,编译器更喜欢非普通字符串.

This was a weird one. So I removed the verbatim as suggested in the comments and it worked when I used the double backslashes in the string. For some reason the code did not like the verbatim string and was translating the backslashes incorrectly. This resolved the issue. If anyone runs in to this you may need to play with the verbatim/non-verbatim strings because in some circumstances the compiler prefers non-verbatim.

这篇关于C#逐字字符串文字不起作用.反斜杠总是很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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