如何使用转义字符在C#6串插? [英] How to use escape characters with string interpolation in C# 6?

查看:168
本文介绍了如何使用转义字符在C#6串插?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用字符串插值和热爱它,但是我在这里我想包括我的输出反斜杠的问题,但我无法得到它的工作。

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.

我要的是这样的..

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



我想要的组合的输出为

I want the output of combo to be

myDomain\myUserName

我所得到的是一个语法错误\作为一个转义字符。如果我把\\那么snytax错误消失,但输出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

我怎么能包括在插值字符串转义字符?

How can I include escaped characters in an interpolated string?

推荐答案

您也可以做到这一点,同时使用$ @。该顺序很重要。

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

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

这篇关于如何使用转义字符在C#6串插?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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