常数错误的新行 [英] New Line in Constant error
问题描述
我正在编写一个C#代码,其中我正在从数据库中获取值并使用它。
我面临的问题如下。
I am writing a c# code in which i am fetching values from the database and using it. The problem i am facing is as below.
如果我从数据库中获取的值是:
If my fetched value from the database is :
string cat1 = "sotheby's";
现在在使用这只猫时,我想在单引号之前插入一个转义字符,以实现这一点编写以下代码:
now while using this cat i want to insert an escape character before single quote, to achieve this i have written the below code:
string cat1 = "sotheby's";
if (cat1.Contains("'")) {
var indexofquote = cat1.IndexOf("'");
cat1.Insert(indexofquote-1,"\");
var cat2 = cat1;
}
错误在插入行(反斜杠(转义字符))中出现。错误是常量中的换行。
请帮助我纠正此错误。
The error rises in insert line, the backslash(escape character). The error is New Line in Constant. Please help me how to correct this error.
推荐答案
您不能只写 C#代码中的
。这将产生常数不变的新行错误,因为它转义了第二个引号,因此不计在内。而且字符串未关闭。
You can't just write "\"
in C# code. That will produce the "New Line in Constant" error because it 'escapes' the second quote so that it doesn't count. And the string isn't closed.
使用 \\
(转义 \
本身)
或使用 @ \
(逐字字符串)。
Use either "\\"
(escaping the \
with itself)
or use @"\"
(a verbatim string).
这篇关于常数错误的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!