Reputation: 4388
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.
Upvotes: 7
Views: 4885
Reputation: 36649
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);
}
Upvotes: 0
Reputation: 4789
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.
Upvotes: 2
Reputation: 14468
var s = System.String.IsNullOrEmpty(param2) ? string.Format(...) : string.Format(...)
Upvotes: 0
Reputation: 42125
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);
}
Upvotes: 1
Reputation: 4789
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.
Upvotes: 2
Reputation: 33098
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)
Upvotes: 4