如何在C#6中使用带字符串插值的转义字符?

时间:2022-02-01 22:27:39

I've been using string interpolation and loving it, however I have an issue where I am trying to include a backslash in my output, but am not able to get it to work.

我一直在使用字符串插值并喜欢它,但我有一个问题,我试图在我的输出中包含反斜杠,但我无法让它工作。

What I want is something like this..

我想要的是这样的......

var domain = "mydomain";
var userName = "myUserName";
var combo = $"{domain}\{userName}"

I want the output of combo to be

我想要组合的输出

myDomain\myUserName

What I am getting is a syntax error about the \ being an escape character. If I put in \\ then the snytax error is gone, but the output is myDomain\\myUsername

我得到的是关于\作为转义字符的语法错误。如果我输入\\那么snytax错误消失了,但输出是myDomain \\ myUsername

How can I include escaped characters in an interpolated string?

如何在插值字符串中包含转义字符?

6 个解决方案

#1


56  

Escaping with a backslash(\) works for all characters except a curly brace.

使用反斜杠(\)进行转义适用于除大括号外的所有字符。

If you are trying to escape a curly brace ({ or }), you must use {{ or }} per https://msdn.microsoft.com/en-us/library/dn961160.aspx.

如果您试图逃避花括号({或}),则必须按照https://msdn.microsoft.com/en-us/library/dn961160.aspx使用{{或}}。

... All occurrences of double curly braces (“{{“ and “}}”) are converted to a single curly brace.

...所有出现的双花括号(“{{”和“}}”)都会转换为单花括号。

#2


48  

You can do this, using both the $@. The order is important.

你可以使用$ @来做到这一点。订单很重要。

var combo = $@"{domain}\{userName}";

#3


3  

$"{domain}\\{user}"

Works fine - escaping works as usual (except when escaping {). At least on .NET 4.6 and VS 14.0.22823 D14REL.

工作正常 - 逃避正常工作(逃避{除外)。至少在.NET 4.6和VS 14.0.22823 D14REL上。

If it doesn't work for some reason (maybe you're using an older version of the compiler?), you could also try being more explicit:

如果由于某种原因它不起作用(也许你正在使用旧版本的编译器?),你也可以尝试更明确:

$"{domain}{@"\"}{user}"

#4


0  

If I did not missunderstood. The solution is real simple

如果我没有错过了解。解决方案非常简单

var domain = "mydomain";
var userName = "myUserName";
var combo = $"{{{domain}}}\\{{{userName}}}";
Console.WriteLine(combo);

I share the birdamongmen answer as well good reference provided there. Hope it is helpfull to you. My 5 cents

我分享了鸟类的回答以及那里提供的好参考。希望它对你有所帮助。我的5美分

#5


0  

Hi the rule for escape the back slash in interpolated string is duplicate the back slash:

嗨逃脱规则插值字符串中的反斜杠是重复斜杠:

var domain = "mydomain";
var userName = "myUserName";
var combo = $"{domain}\\{userName}";

如何在C#6中使用带字符串插值的转义字符?

but if you also use the interpolated string as verbatim string then you don't need to escape the back slash:

但是如果你还使用插值字符串作为逐字字符串,那么你不需要转义反斜杠:

var domain = "mydomain";
var userName = "myUserName";
var combo = $@"{domain}\{userName}";

and you get the same:

你得到了同样的东西:

如何在C#6中使用带字符串插值的转义字符?

For a tutorial about interpolated string: see video interpolated string

有关插值字符串的教程:请参阅视频插值字符串

#6


-1  

Eduardo is correct. You escape curly braces by doubling up. Therefore, if you wanted to output the domain variable as {mydomain} you would need to do:

爱德华多是对的。你通过加倍来逃避花括号。因此,如果您想将域变量输出为{mydomain},则需要执行以下操作:

$"{{{domain}}}";

Furthermore, assuming that the current date is 1 Sept 2016, doing this:

此外,假设当前日期是2016年9月1日,执行此操作:

$"The date is {DateTime.Now}";

would output something like "The date is 2016/09/01 3:04:48 PM" depending on your localization. You can also format the date by doing:

会输出类似“日期是2016/09/01 3:04:48 PM”的内容,具体取决于您的本地化。您还可以通过执行以下操作格式化日期:

$"The date is {DateTime.Now : MMMM dd, yyyy}";

which would output "The date is September 1, 2016". Interpolated strings are much more readable. Good answer Eduardo.

输出“日期是2016年9月1日”。插值字符串更易读。 Eduardo很好的答案。

#1


56  

Escaping with a backslash(\) works for all characters except a curly brace.

使用反斜杠(\)进行转义适用于除大括号外的所有字符。

If you are trying to escape a curly brace ({ or }), you must use {{ or }} per https://msdn.microsoft.com/en-us/library/dn961160.aspx.

如果您试图逃避花括号({或}),则必须按照https://msdn.microsoft.com/en-us/library/dn961160.aspx使用{{或}}。

... All occurrences of double curly braces (“{{“ and “}}”) are converted to a single curly brace.

...所有出现的双花括号(“{{”和“}}”)都会转换为单花括号。

#2


48  

You can do this, using both the $@. The order is important.

你可以使用$ @来做到这一点。订单很重要。

var combo = $@"{domain}\{userName}";

#3


3  

$"{domain}\\{user}"

Works fine - escaping works as usual (except when escaping {). At least on .NET 4.6 and VS 14.0.22823 D14REL.

工作正常 - 逃避正常工作(逃避{除外)。至少在.NET 4.6和VS 14.0.22823 D14REL上。

If it doesn't work for some reason (maybe you're using an older version of the compiler?), you could also try being more explicit:

如果由于某种原因它不起作用(也许你正在使用旧版本的编译器?),你也可以尝试更明确:

$"{domain}{@"\"}{user}"

#4


0  

If I did not missunderstood. The solution is real simple

如果我没有错过了解。解决方案非常简单

var domain = "mydomain";
var userName = "myUserName";
var combo = $"{{{domain}}}\\{{{userName}}}";
Console.WriteLine(combo);

I share the birdamongmen answer as well good reference provided there. Hope it is helpfull to you. My 5 cents

我分享了鸟类的回答以及那里提供的好参考。希望它对你有所帮助。我的5美分

#5


0  

Hi the rule for escape the back slash in interpolated string is duplicate the back slash:

嗨逃脱规则插值字符串中的反斜杠是重复斜杠:

var domain = "mydomain";
var userName = "myUserName";
var combo = $"{domain}\\{userName}";

如何在C#6中使用带字符串插值的转义字符?

but if you also use the interpolated string as verbatim string then you don't need to escape the back slash:

但是如果你还使用插值字符串作为逐字字符串,那么你不需要转义反斜杠:

var domain = "mydomain";
var userName = "myUserName";
var combo = $@"{domain}\{userName}";

and you get the same:

你得到了同样的东西:

如何在C#6中使用带字符串插值的转义字符?

For a tutorial about interpolated string: see video interpolated string

有关插值字符串的教程:请参阅视频插值字符串

#6


-1  

Eduardo is correct. You escape curly braces by doubling up. Therefore, if you wanted to output the domain variable as {mydomain} you would need to do:

爱德华多是对的。你通过加倍来逃避花括号。因此,如果您想将域变量输出为{mydomain},则需要执行以下操作:

$"{{{domain}}}";

Furthermore, assuming that the current date is 1 Sept 2016, doing this:

此外,假设当前日期是2016年9月1日,执行此操作:

$"The date is {DateTime.Now}";

would output something like "The date is 2016/09/01 3:04:48 PM" depending on your localization. You can also format the date by doing:

会输出类似“日期是2016/09/01 3:04:48 PM”的内容,具体取决于您的本地化。您还可以通过执行以下操作格式化日期:

$"The date is {DateTime.Now : MMMM dd, yyyy}";

which would output "The date is September 1, 2016". Interpolated strings are much more readable. Good answer Eduardo.

输出“日期是2016年9月1日”。插值字符串更易读。 Eduardo很好的答案。