2

This questions really isn't hard to understand. I have always wondered this and wondered if possible, how could it be done. Well this is sort what I'd like to do if possible:

    int number = 50;
    string text = "The number is %number";

Where I wrote %number in the string above, I would like the value of number to be inserted into the string, because the way I would usually go about doing something like this would be like:

    int number = 50;
    string text = "The number is " + number.ToString();

Which yes, the way I integrated number into the string above is an okay way of doing so, but I have always wondered, is it possible to do something such as the first example I wrote, where to put a value of an object into a string all you would have to do is write some type of character or string (used to reference the object), along with the name of the object into a string to get a result of a string with the value of the object in it? Or is there anything sort of like this you're able to do?

Jake
  • 181
  • 2
  • 15
  • Just a note, most (all?) cases when you're performing simple string concatenation you can simply just add them without explicitly calling `ToString()` or using `String.Format`. For example: `string text = "The number is " + number;` is sufficient. – Chris Sinclair Mar 02 '13 at 19:34
  • 1
    @ChrisSinclair To come up with an example where it will not work, what about: `string text = currency + amount + " was successfully transferred.";` where neither `currency` nor `amount` is of compile-time type `string`. Then the compiler will complain that the operator `+` cannot be applied to types `MyNamespace.Currency` and `decimal` (for example). An ugly fix might be `string text = currency + (amount + " was successfully transferred.");`. This is not a realistic example, probably. – Jeppe Stig Nielsen Mar 02 '13 at 21:20
  • @JeppeStigNielsen Good thinking. I had not considered combining two non-strings together in that order. If the string literal portion happened to come first (`string text = "Transfer success: " + currency + amount;`) it would work fine. Guess it goes to show how rare such occurrences are (or more likely, they're mundane enough to resolve that I can't really recall them) – Chris Sinclair Mar 02 '13 at 23:22

2 Answers2

5

You would use string.Format:

string text = string.Format("The number is {0}", number);

Note that all objects have a ToString method, which means that all objects can be used as arguments to string.Format, however the default response from ToString is to return the full name of the type, which might not make much sense.

For instance, this:

public class Dummy
{
}

var d = new Dummy();
string text = string.Format("The dummy is {0}", d);

will assign something like the following value to text:

"The dummy is Your.Namespace.Dummy";

You have two options:

  1. Override ToString and return something meaningful for your new type
  2. Read off a property or something instead, e.g.:

    string text = string.Format("The dummy is {0}", d.SomeProperty);
    

Also note that string.Format can take multiple arguments:

int a = 10;
int b = 20;
int c = a + b;
string text = string.Format("{0} + {1} = {2}", a, b, c);

There's a lot more to string.Format than what I have shown here, so click the link to the documentation (first line of this answer) to learn more.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • Thank you very much, I really do appreciate your answer and everyone else's. You were very quick to respond and I really appreciate it. The `string.Format` will definitely come in handy. I just wish there was a much faster way of writing it, I'm not trying to be lazy, I'd just rather save time writing code. Well thanks again. :) – Jake Mar 02 '13 at 19:27
0

You probably need to check Sting.Format

string text = String.Format("The number1 is {0},number2 is {1}", number1, number2);

It is also worth to check this discussion regarding When is it better to use String.Format vs string concatenation? `

Community
  • 1
  • 1
ssilas777
  • 9,672
  • 4
  • 45
  • 68