双引号字符串替换在C# [英] Double quote string replace in C#
问题描述
如何更换下面的字符串在C#
电流:
约翰KGEN格雷格
目标:
约翰ķ\GEN\格雷格
这是错误的,因为我不是逃避它正确:
S = s.Replace( ,\);
什么是语法与替换引号\(斜线)?
任何帮助,将不胜感激。
感谢
S = s.Replace(\,\\\);
或
S = s.Replace(@,@\,);
在第一个例子中的有可以用一个反斜杠转义,因为它本来结束的字符串。同样,在替换字符串
\\
需要通过转义转义字符以产生一个反斜杠。
在第二个例子中逐字字符串时,它们被写成 @...
。在这些文字不转义序列的认可,让您编写包含很多个反斜杠在一个更清洁的方式(比如正则表达式)字符串。的作品有唯一的转义序列,
的一个。
How to replace the below string in C#
Current:
"John K "GEN" Greg"
The Goal:
"John K \"GEN\" Greg"
This is wrong because I'm not escaping it properly:
s = s.Replace(""","\"");
What is syntax for replacing quotes with \ (slash)?
Any help would be appreciated.
Thanks
s = s.Replace("\"", "\\\"");
or
s = s.Replace(@"""", @"\""");
In the first example the "
has to be escaped with a backslash as it would otherwise end the string. Likewise, in the replacement string \\
is needed to yield a single backslash by escaping the escape character.
In the second example verbatim string literals are used, they are written as @"..."
. In those literals no escape sequences are recognized, allowing you to write strings that contain lots of backslashes in a much cleaner way (such as regular expressions). The only escape sequence that works there is ""
for a single "
.
这篇关于双引号字符串替换在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!