如何在资源文件中使用字符串插值?

编程入门 行业动态 更新时间:2024-10-28 18:30:06
本文介绍了如何在资源文件中使用字符串插值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用资源文件发送电子邮件.在我的资源文件中,我使用了一个变量"EmailConfirmation",其值为"Hello {userName} ..."

I would like to use a resource file to send an email. In my resource file I used a variable "EmailConfirmation" with the value "Hello {userName} ... "

在我的课堂上,我使用了:

In my class I used:

public static string message (string userName) { return Resource.WebResource.EmailConfirmation }

问题在于返回值显示为"Hello {userName}",而不是"Hello Toto".

The problem is that the return says "Hello {userName}" instead of "Hello Toto".

推荐答案

您不能在资源的上下文中使用字符串插值.但是,您可以通过使用string.Format实现所需的功能.将这样的内容写入您的资源文件:

You can't make use of string interpolation in the context of resources. However you could achieve that you want by making use of string.Format. Write to your resource file something like this:

Hello {0}

,然后按如下所示使用它:

and then use it like below:

public static string message (string userName) { return string.Format(Resource.WebResource.EmailConfirmation, userName); }

更新

您可以根据需要添加任意数量的参数.例如:

You can add as many parameters as you want. For instance:

Hello {0}, Confirm your email: {1}

然后您可以将其用作:

string.Format(Resource.WebResource.EmailConfirmation , userName , HtmlEncoder.Default.Encode(link))

更多推荐

如何在资源文件中使用字符串插值?

本文发布于:2023-11-12 19:34:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582335.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   插值   文件   如何在   资源

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!