1

Do you know another more proper way to do the same things ?

string initialTemplate = "{0}-{1}";
string template = string.Format(initialTemplate, "first", "{0}");
string answer = string.Format(template, "second");

Also the following way has actually known, but in my current case unfortunatelyI can't use that method(i think that that way more proper and the logic more clear):

string initialTemplate = "{0}-{{0}}";
string template = string.Format(initialTemplate, "first");
string answer = string.Format(template, "second");

Maybe is there another hint how to do that?

UPDATE I'm so sorry but from yours answers I've learnt that my question wasn't enough clear. So I've added a little bit more description.

My situation:

//that template is actually placed in *.resx file
//I want storing only one template and use that in different situations
public const string InitialTemplate = "{0}-{1}";

public static string GetMessage(string one, string two)
{
    return string.Format(InitialTemplate, one, two);
}

public static string GetTemplate(string one)
{
    return string.Format(InitialTemplate, one, "{0}");
}

//or morew universal way
public static string GetTemplate(params object[] args)
{
    return string.Format(InitialTemplate, args, "{0}");
}

static void Main(string[] args)
{
    //in almost all cases in my project i need to use string.format like this
    string message = GetMessage("one", "two");

    //but there is another way where i have to use 
    //the template have already been assigned first argument
    //the result must be "one-{0}"
    string getTemplateWithAssignedFirstArg = GetTemplate("one");
}

Do you know more proper way for that kind of situation ?

1
  • 1
    Why can't you use that second method? Do you not have write-access to initialTemplate? Commented May 17, 2016 at 17:31

3 Answers 3

4

If you are using C# 6 you can also use string interpolation. https://msdn.microsoft.com/en-us/library/dn961160.aspx

var answer = $"{firstVar}-{secondVar}";
Sign up to request clarification or add additional context in comments.

Comments

3
string initialTemplate = "{0}-{1}";
string answer = string.Format(initialTemplate, "first", "second");

Should do the trick. Or cut out the middle man with:

string answer = string.Format("{0}-{1}", "first", "second");

Comments

1

String.Format is a very useful convenience, but I'd be wary of using it to build format strings that you're going to use to create other format strings. Someone trying to maintain that code, figure out what's going on, and perhaps modify it will be baffled. Using String.Format that way is technically possible, and there could even be scenarios where it's useful, but it's probably just going to result in something that works but is very difficult to understand and debug.

My first suggestion would be to use a StringBuilder. Even when you're appending to the StringBuilder you can use String.Format if needed to create the individual strings.

I wonder if perhaps what you describe in the question is taking place across multiple methods (which is why you might be building your format string in steps.) If that's the case, I recommend not building the string in steps like that. Don't actually start building the string until you have all of the data that you need together, and then build the string at once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.