本文介绍了为什么我的字典为字符串添加额外的转义字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! App.config:App.config:<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="1" value="An error occured.\r\nPlease try again later." /> </appSettings></configuration>代码:Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();string key = "1";keyValuePairs.Add(key, ConfigurationManager.AppSettings["key"]);if (keyValuePairs.ContainsKey(key)){ // when I hover over the object, keyValuePairs, in my debugger, I see, "An error occured.\r\nPlease try again later." string value1 = keyValuePairs[key]; // now when I hover over the local variable, value, I see, "An error occured.\\r\\nPlease try again later."} 我很好奇上述代码为什么添加了转义字符到\\\\来使它成为\\\\ 我也喜欢知道如何避免获得额外的\字符。I'd also like to know how to avoid getting the extra "\" characters.推荐答案从app.config中读取字符串,我认为它类似于以下内容:If you are reading the string from the app.config, I assume it's something like the following:<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="errorString" value="An error occured.\r\nPlease try again later."/> </appSettings></configuration>在这种情况下,您的示例代码应如下所示:In which case your sample code should look like this:keyValuePairs.Add("1", @"An error occured.\r\nPlease try again later.");从那里应该是显而易见的为什么调试器正在显示(特别是如果你读 JaredPar的答案。And from there it should be obvious why the debugger is displaying as it does (especially if you read JaredPar's answer.您最好使用资源字典存储字符串,这很容易让您在VS编辑器中插入换行符。You're better off using a resource dictionary for storing the string, which will easily let you insert newline characters in the VS editor.编辑:我试过了以下内容: Just on a whim, I tried the following:<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="errorString" value="An error occured.Please try again later."/> </appSettings></configuration>编译好并产生正确的结果。Compiled fine and produced the correct results. 这篇关于为什么我的字典为字符串添加额外的转义字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!