在字符串中间插入变量值 [英] Insert variable values in the middle of a string

查看:154
本文介绍了在字符串中间插入变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中:如果我要创建这样的消息:
我们为您提供了以下航班: A,B,C,D航班。

In C#: If I want to create a message like this: "Hi We have these flights for you: Flight A,B,C,D. Which one do you want"

其中,粗体部分是动态的,我在运行时传递其值,但其左右部分是固定的。我可以创建类似LeftMessage +这些变量+ RightMessage的东西来创建它。
但是我想知道是否有一种方法可以一次完成所有操作而无需创建两个单独的左右消息?

where just the section in bold is dynamic and I pass its value at run time, but its left and right parts are fixed. I can create something like LeftMessage + those variables + RightMessage to create this. But I was wondering if there is a way of doing it all at once without the need to create two separate left and right messages?

出于翻译目的,我正在将左右消息放在字符串资源中,所以现在我有两个单独的字符串资源。有没有办法一次完成所有操作?

For translation purposes I am putting those left and right messages inside string resources so now I have two separate string resources. Is there a way to do it all at once?

推荐答案

您可以使用 string.Format

string template = "Hi We have these flights for you: {0}. Which one do you want";
string data = "A, B, C, D";
string message = string.Format(template, data);

您应该从资源文件和<$ c中加载模板 $ c> data 是您的运行时值。

You should load template from your resource file and data is your runtime values.

但是,如果要翻译成多种语言,请小心:在某些情况下,您将需要不同的标记( {0} )以不同的语言显示。

Be careful if you're translating to multiple languages, though: in some cases, you'll need different tokens (the {0}) in different languages.

这篇关于在字符串中间插入变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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