C#需要帮助将两个反斜杠转换为一个 [英] C# need help converting two back slashes to one

查看:370
本文介绍了C#需要帮助将两个反斜杠转换为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,因为标题说我有2个反斜杠转换为1的问题。



所以这就是我在这个项目中我想要的文本框和组合框,文本框。文本框包含文件夹名称,组合框包含存储目录,例如;

Combobox会说HDD:\和文本框会说Games\,现在点击按钮我想将位置和文件夹名称保存到Json文件中。但保存到Json文件后,它将显示如下。



Hello there as the title say I am having problem with converting 2 back slashes to 1.

So here what I am I trying to in the project I have a textbox and a combobox, textbox. The textbox contain the folder name and the combobox contain the storage directory so for example;
Combobox will say HDD:\ and the textbox will say Games\, now with a button click i will like to save the location and folder name to a Json file. But after saving to the Json file it will show up like this.

"quick": {
    "LaunchTitle1": "Black Ops 3",
    "LaunchDir1": "Games\\",
    "LaunchName1": "Black Ops 3",
    "LaunchStorage1": "HDD:\\",
  },





但问题是它只能使用1个反斜杠而不是2个。



现在是我的编码片段。





But the problem is that it will only work with 1 back slash not two.

Now a snip of my coding.

string temp = tbDir.Text;
           temp = temp.Replace(@"\\", @"\");

           string temp1 = comboBoxEdit1.Text;
           temp1 = temp1.Replace(@"\\", @"\");

           Program.Settings.quick.LaunchStorage1 = temp1;
           Program.Settings.quick.LaunchTitle1 = lvDir.FocusedItem.Text;
           Program.Settings.quick.LaunchName1 = lvDir.FocusedItem.Text;
           Program.Settings.quick.LaunchDir1 = temp;
           Program.Settings.Save();





请记住我刚刚开始编程(如果你把它编程为编程)2个月前所以请放轻松我,我有谷歌这个问题,所有答案都使用替换方法,我试图做。感谢你提前的帮助。



我尝试过:





Please bare in mind I have only started to program(If you class this as programming) 2 months ago so please take it easy on me, I have google this issue and all the answers are use the replace method, which I am trying to do. Thanks for the help in a advance.

What I have tried:

temp = temp.Replace(@"\\", @"\");

          string temp1 = comboBoxEdit1.Text;
          temp1 = temp1.Replace(@"\\", @"\");

推荐答案

别管它!这就是它应该做的,当JSON数据被处理并读回你的应用程序时它将被颠倒。



在JSON数据中(以及许多其他数据)例如C#之类的系统反斜杠充当转义字符,允许您通过在字符或代码之前添加\将特殊字符输入到字符串中。例如,您使用它来引入双引号而不结束字符串:

Leave it alone! That is what it is supposed to do, and it will be reversed when the JSON data is processed and read back into your application.

In JSON data (and many other systems such as C# for example) the back slash acts as an "escape" character, allowing you to enter "special characters" into a string by adding a '\' before the character or code. For example, you use it to introduce a double quote without ending the string:
"hello \"Member 13199106\""

哪个创建像这样的字符串:

Which creates a string like this:

hello "Member 13199106"

这意味着要插入反斜杠,必须将其写为反斜杠(转义字符)然后是反斜杠(插入):\\

That means that to insert a backslash it has to be written as a backslash (escape character) followed by a backslash (to insert): "\\"


字符串中的两个'\'是json标准的一部分。



要读取json文件,请使用库而不是直接读取json文件(它们为您处理转换)

试试这个:fastJSON [ ^ ]
Two '\' in the string is part of the json standard.

To read the json file use a library instead of reading the json file directly (they handle the conversion for you)
Try this : fastJSON[^]


\ 在json文件中有特殊含义,编码为 \\

如果您使用适当的阅读器阅读json,它将自动ally解码为变量中的单个 \



\\的原因\\\ \ 转义字符。如果在json文件中遇到 \,则表示是字符串的一部分而不是字符串结束。



所以唯一的解决方案是使用合适的json阅读器来确保正确解码。



您的代码片段不允许我们知道您在哪里读取json文件或存储在哪里变量。
\ have a special meaning in a json file and is encoded as \\.
If you read the json with a proper reader, it will automatically decode to a single \ in the variables.

the reason of \\ is that \ is an escape char. If you encounter \" in a json file, it means that the " is part of the string instead of being the end of string.

So the only solution is to use a proper json reader to ensure correct decoding.

Your code snipset do not allow us to know where you read the json file or where it is stored od what is in the variables.


这篇关于C#需要帮助将两个反斜杠转换为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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