我一直在關注創建 CSV 文件的一些方法,但顯然老師在代碼的一部分中存在一些問題。實際上有一種方法可以替換奇怪的字符并轉義逗號,因為它正在創建 CSV。它的代碼如下所示:private string PrepareInputForFile(string input){
input = input.Replace('?', 'i')
.Replace('?', 'c')
.Replace('?', 'o')
.Replace('?', 's')
.Replace('ü', 'u')
.Replace('?', 'g')
.Replace('?', 'I')
.Replace('?', 'C')
.Replace('?', 'O')
.Replace('?', 'S')
.Replace('ü', 'U')
.Replace('?', 'G')
.Replace(""", """") // this line is issue
.Trim();
if (input.Contains(","))
{
input = """ + input + """; // and this line also!
}
return input;
}實際上拋出了一個錯誤:將文本表示為 UTF-16 代碼單元序列。; 預期的。那么我該如何修復此代碼,使其可能真正起作用(轉義逗號等)。
2 回答

一只甜甜圈
TA貢獻1836條經驗 獲得超5個贊
您必須使用"\""
而不是"""
將字符定義"
為字符串的一部分。您的代碼應如下所示:
.Replace("\"", "\"\"") // this line is issue

慕的地8271018
TA貢獻1796條經驗 獲得超4個贊
您必須轉義 C# 字符串中的雙引號:
... .Replace("\"", "\"\""); ... input = "\"" + input + "\"";
- 2 回答
- 0 關注
- 148 瀏覽
添加回答
舉報
0/150
提交
取消