問題描述
我正在編寫一個 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).
這篇關于恒定錯誤中的新行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!