转义字符串中的双引号 [英] Escape double quotes in a string

查看:43
本文介绍了转义字符串中的双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

双引号可以这样转义:

string test = @"He said to me, ""Hello World"". How are you?";

但这涉及向字符串添加字符 ".是否有 C# 函数或其他方法来转义双引号,以便不需要更改字符串?

But this involves adding character " to the string. Is there a C# function or other method to escape double quotes so that no changing in string is required?

推荐答案

没有

要么按原样使用逐字字符串文字,要么使用反斜杠转义 ".

Either use verbatim string literals as you have, or escape the " using backslash.

string test = "He said to me, "Hello World" . How are you?";

字符串在任何一种情况下都没有改变 - 其中有一个 escaped " .这只是告诉 C# 该字符是该字符的一部分的一种方式字符串而不是字符串终止符.

The string has not changed in either case - there is a single escaped " in it. This is just a way to tell C# that the character is part of the string and not a string terminator.

这篇关于转义字符串中的双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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