2

我目前正在尝试从我的 C# 代码中的字符串中删除不需要的字符。

删除 \ 字符,但删除太多。下面是一个字符串的例子

"[{ \"属性\" : { \"SR_ID\" : \"200003172375\",
\"Fath_SR\" : \"EH0036\", \"UPRN\" : \"100100024250\",
\"Eastings\ " : \"260376\", \"Northings\" : \"358150\",
\"Disgrifiad\" : \"测试\", \"日期\" : \"25/01/2012\",
\"时间\" : \"11:36\" }, \"几何\" : { \"x\" : 270315, \"y\" : 345828 } }]"

我正在尝试删除 \ 字符,但留下“。我能够删除 \ 的唯一方法是使用

 sReturn = sReturn.Replace("\"",String.Empty);

但这会删除 " 字符。

我已经尝试了以下两种尝试,但由于某种原因,它不想按照它所说的去做!

 sReturn = sReturn.Replace(@"\",String.Empty);
 sReturn = sReturn.Replace("\\",String.Empty);

有没有办法可以用“替换\”?

4

5 回答 5

1

也许:

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

逐字字符串文字允许使用\char。

编辑:我刚刚看到你已经尝试过了。这只是 Visual Studio 中的显示问题。

于 2013-01-25T16:37:28.423 回答
1

\"是一个转义的字符串序列。实际的字符串只是引号 ("),但包含反斜杠以表明它已被转义,并且引号 (") 实际上并不是字符串的结尾(它是字符串的一部分,而不是定义结尾的字符)细绳)。

尝试将字符串打印到控制台或在消息框中显示;这将显示字符串实际上是什么而不显示转义字符串。

于 2013-01-25T16:39:10.443 回答
1

Assuming you really DO have \" characters in your string and aren't just looking at an escaped representation, you can simply do:

sReturn = sReturn.Replace("\\\"","\"");

If you want to unescape all possible types of escape sequences, use:

sReturn = Regex.Unescape(sReturn);

But as others have pointed out, you probably don't really have these characters, and are just looking at a representation where it is shown in the escaped form, such as in visual studio debugger.

于 2013-01-25T16:43:43.630 回答
0

The slash( \ ) is an escape character letting the compiler know that the next character is supposed to be part of the string. This is an important notation because otherwise the string would end when the compiler gets to the ending quotation mark. Indeed, if you examine the string under different compiler configuration (if you looked at the library in a VB.Net project, for example), you won't see the slash there.

于 2013-01-25T16:40:06.140 回答
0

In fact those characters (\) are \" ("). If you print the string you will see there is no '\' character. So you can leave it as is. If you want to compare these spatial information to other, there must be no problem.

于 2013-01-25T16:41:29.977 回答