Insert variable values in the middle of a string

C#String

C# Problem Overview


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"

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?

C# Solutions


Solution 1 - C#

There's now (C# 6) a more succinct way to do it: string interpolation.

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. >

Solution 2 - C#

You can use 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 template from your resource file and data is your runtime values.

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

Solution 3 - C#

Use String.Format

Pre 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

string data = "FlightA, B,C,D";
var str = $"Hi We have these flights for you: {data}. Which one do you want?";

http://www.informit.com/articles/article.aspx?p=2422807

Solution 4 - C#

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)

Solution 5 - C#

1 You can use string.Replace method

var sample = "testtesttesttest#replace#testtesttest";
var result = sample.Replace("#replace#", yourValue);

2 You can also use string.Format

var result = string.Format("your right part {0} Your left Part", yourValue);

3 You can use Regex class

Solution 6 - C#

I would use a StringBuilder class for doing string manipulation as it will more efficient (being mutable)

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?");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarkNightFanView Question on Stackoverflow
Solution 1 - C#VimesView Answer on Stackoverflow
Solution 2 - C#Dan PuzeyView Answer on Stackoverflow
Solution 3 - C#codingbizView Answer on Stackoverflow
Solution 4 - C#Paolo FalabellaView Answer on Stackoverflow
Solution 5 - C#Aghilas YakoubView Answer on Stackoverflow
Solution 6 - C#Joydeep SarkarView Answer on Stackoverflow