64

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?

Val Berthe
  • 1,899
  • 1
  • 18
  • 33
DarkNightFan
  • 1,025
  • 2
  • 9
  • 15
  • Yes look up String.Join() method example String.Join(', ', someVariable) or String.Format("message {0}. and use the following param counter(s) for the rest of the text params {1} {2} {3}, etc – MethodMan Oct 23 '12 at 15:12

7 Answers7

141

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.

Legolas
  • 894
  • 1
  • 11
  • 25
Vimes
  • 10,577
  • 17
  • 66
  • 86
66

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • re: tokens - I can't find any info on this, do you mean that if using a different language I would need to use the equivalent of {0} in the other language? – A_L Feb 03 '15 at 12:40
39

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

codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • my code tool (resharper) suggests an autocorrect from the first form to the second one, indicating that that is (by default) the preferred syntax. – Andrew Hill Jan 05 '18 at 01:16
11
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)

Paolo Falabella
  • 24,914
  • 3
  • 72
  • 86
2

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

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
0

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

A consideration to not use string interpolation even after C# 6.0 is logging (e.g. Microsoft.Extensions.Logging or Serilog). My preference is the message template:

var flights = "A, B, C, D";
logger.Information(string.Format("Flights: {0}. Please choose:", flights));

Instead of string interpolation:

logger.Information($"Flights: {flights}. Please choose:");

Because log services, like Azure's Application Insights or Amazon's CloudWatch Logs, use the parameters of the template and allow for filtering on those parameters as properties on the log message. With string interpolation you lose that information, because it's converted to a string.

For more explanation, see Nick Chapsas's video.

Jonne Kleijer
  • 592
  • 5
  • 14