views:

81

answers:

6

I am attempting to isolate (for localization purposes) the formatting of some messages. In one of the cases, I have several parameters, some of which may be an empty string. An example is probably called for here....

If the parameters are Parameter one and Parameter two then I want the result to be Some message Parameter one (Parameter two).

If the parameters are Parameter one and string.Empty then I want the result to be Some message Parameter one

If Parameter two was a numeric value, then I could use something like:

String.Format("Test {0}{1:' ('#')'}", "Parameter one", 12);

This operates as I'd expect - specifically if the second parameter is null the output is just Test Parameter one.

Unfortunately I haven't (yet) found a similar option which works with string parameters. Is there one?

Clarification: I am fully aware of numerous ways to get the result I need in code. I specifically want to know if there is a similar built-in mechanism for strings to the numeric one shown above.

+3  A: 

You could always attempt writing your own custom string formatter by implementing IFormatProvider and ICustomFormatter

Then invoke it as

var stringValue = string.Format(new NewCustomStringFormatInfo(),
     "Test {0}{1:' ('#')'}", "Parameter one", 12)
Chris Marisic
Ooh! I like that idea. It's not the built-in option I was hoping for, but it would save me from repeating myself when I need similar functionality elsewhere.
Richard J Foster
This is probably the best answer, so I am marking it as such... Honorable mention should also go to ChrisF who gave the most accurate answer to the question I had intended to ask (i.e. there is no ready-to-use mechanism). In my opinion this is the best answer because it allows me to implement the required operation in one place (the custom formatter) and reuse it. CodeMonkey4hire's extension method suggestion is also a good one, but not as suitable to my current needs.
Richard J Foster
The other answers should go a long way towards giving you the solution on how to code what will be executed in the format string info.
Chris Marisic
A: 
var s = System.String.IsNullOrEmpty(param2) ? string.Format(...) : string.Format(...)
Mau
Unfortunately, while that would work, it's not what I was looking for as I would need to jump through similar hoops each time I needed a similar output.
Richard J Foster
+2  A: 

Depends on your situation, but you could do

string.Format(yourFormatString, paramOne, paramTwo).Replace("()", "");

No guarantees, as it is not fool-proof and makes the large assumption that your resulting string would only have "()" if paramTwo was empty.

Andy_Vulhop
Yes, I certainly could (and probably will need to) do something like that. I just thought it odd that there was apparently a mechanism to add extra formatting for numeric values while there wasn't for string ones. Thanks for the suggestion.
Richard J Foster
+1  A: 

Unless you define a function that encloses a value in brackets, I can't see how you do it inline?

A simple example:

string.Format("Some message {0} {1}", "Parameter one", EncloseInParenthsisIfNotEmpty(""))

public string EncloseInParenthsisIfNotEmpty(string input)
{
    if (string.IsNullOrEmpty(input)) return "";
    return string.Format("({0})", input);
}
Neil Barnwell
+2  A: 

You could create an extension method to help handle this and make it a little more concise.

public static string SomeWellNamedExtension(this string s)
{
    if(string.IsNullOrEmpty(s))
        return "";

    return string.Format("({0})", s);
}

This method will handle the null/empty check and the parens. It's a pretty specialized method, so it's not likely to be useful almost anywhere else. But then your code would be like:

string.Format("Test {0}{1}, paramOne, paramTwo.SomeWellNamedExtension());

However, ymmv. This will affect your format string in that the parens are no longer its responsibility. I can't think of many super elegant ways of handling the use case you are talking detailing.

Andy_Vulhop
A: 

You could write your own wrapper of String.Format (untested):

string MyFormat(string format, params object[] args)
{
  object[] newArgs = new object[args.Length];
  for(int i=0; i<args.Length; i++)
  {
    if(args[i] is string)
    {
      newArgs[i] = String.IsNullOrEmpty(args[i] as string) ? "" : string.Format("({0})", args[i]);
    }
    //numeric cases etc
    else
    { 
      newArgs[i]=args[i];
    }
  }
  return string.Format(format, newArgs);
}
Grzenio