C# 在字符串中间插入变量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13033581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert variable values in the middle of a string
提问by DarkNightFan
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"
在 C# 中:如果我想创建这样的消息:“嗨,我们为您准备了这些航班:A、B、C、D 航班。您想要哪一个”
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?
其中粗体部分是动态的,我在运行时传递它的值,但它的左右部分是固定的。我可以创建像 LeftMessage + 这些变量 + RightMessage 这样的东西来创建它。但是我想知道是否有一种方法可以一次完成所有操作而无需创建两个单独的左右消息?
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?
出于翻译目的,我将这些左右消息放在字符串资源中,所以现在我有两个单独的字符串资源。有没有办法一次性完成所有操作?
采纳答案by Dan Puzey
You can use string.Format:
您可以使用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);
You should load templatefrom your resource file and datais your runtime values.
您应该template从您的资源文件加载并且data是您的运行时值。
Be careful if you're translating to multiple languages, though: in some cases, you'll need different tokens (the {0}) in different languages.
但是,如果您要翻译成多种语言,请小心:在某些情况下,您将需要{0}不同语言的不同标记 (the )。
回答by Aghilas Yakoub
1 You can use string.Replacemethod
1 您可以使用 string.Replace方法
var sample = "testtesttesttest#replace#testtesttest";
var result = sample.Replace("#replace#", yourValue);
2 You can also use string.Format
2 你也可以使用 string.Format
var result = string.Format("your right part {0} Your left Part", yourValue);
3 You can use Regex class
3 您可以使用 Regex 类
回答by Paolo Falabella
String.Format("Hi We have these flights for you: {0}. Which one do you want",
flights);
EDIT: you can even save the "template" string separately (for instance you could store it in a configuration file and retrieve it from there), like this:
编辑:您甚至可以单独保存“模板”字符串(例如,您可以将其存储在配置文件中并从那里检索它),如下所示:
string flights = "Flight A, B,C,D";
string template = @"Hi We have these flights for you: {0}. Which one do you want";
Console.WriteLine(String.Format(template, flights));
EDIT2: whoops, sorry I see that @DanPuzey had already suggested something very similar to my EDIT (but somewhat better)
EDIT2:哎呀,抱歉,我看到@DanPuzey 已经提出了与我的 EDIT 非常相似的建议(但要好一些)
回答by codingbiz
Use String.Format
Pre C# 6.0
C# 6.0 之前的版本
string data = "FlightA, B,C,D";
var str = String.Format("Hi We have these flights for you: {0}. Which one do you want?", data);
C# 6.0 -- String Interpolation
C# 6.0 --字符串插值
string data = "FlightA, B,C,D";
var str = $"Hi We have these flights for you: {data}. Which one do you want?";
回答by Vimes
There's now (C# 6) a more succinct way to do it: string interpolation.
现在(C# 6)有一种更简洁的方法来做到这一点:字符串插值。
From another question's answer:
从另一个问题的答案:
In C# 6 you can use string interpolation:
string name = "John"; string result = $"Hello {name}";The syntax highlighting for this in Visual Studio makes it highly readable and all of the tokens are checked.
在 C# 6 中,您可以使用字符串插值:
string name = "John"; string result = $"Hello {name}";Visual Studio 中的语法突出显示使其具有高度可读性,并且所有标记都经过检查。
回答by Joydeep Sarkar
I would use a StringBuilder class for doing string manipulation as it will more efficient (being mutable)
我会使用 StringBuilder 类进行字符串操作,因为它会更有效(可变)
string flights = "Flight A, B,C,D";
StringBuilder message = new StringBuilder();
message.Append("Hi We have these flights for you: ");
message.Append(flights);
message.Append(" . Which one do you want?");

