When using String.Format, there is an easy way to add parentheses around a string value if it is not null or empty

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

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

If the parameters are Parameter 1 and String. Then, I want the result to be Some message Parameter one

If parameter 2 was a numeric value, I could use something like:

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

This works the way I expected - especially if the second parameter is zero, the output is only test parameter 1.

Unfortunately, I have not yet found a similar option that works with string parameters. There is one?

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

+7
string c # formatting
source share
6 answers

You can always try writing your own custom string formatter by doing IFormatProvider and ICustomFormatter

Then call it like

 var stringValue = string.Format(new NewCustomStringFormatInfo(), "Test {0}{1:' ('#')'}", "Parameter one", 12) 
+4
source share

Depends on your situation, but you can do

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

There is no guarantee, because it is not flawless and makes a big assumption that your resulting string will only have "()" if paramTwo was empty.

+2
source share

You can create an extension method to 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 null / empty and parens checks. This is a fairly specialized method, so it is unlikely to be useful almost anywhere. But then your code will look like this:

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

However, ymmv. This will affect your format string in the sense that parens are no longer its responsibility. I cannot think of many elegant ways to handle the case you are using, which you are talking about in detail.

+2
source share

If you do not define a function that brackets the value, I donโ€™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); } 
+1
source share
 var s = System.String.IsNullOrEmpty(param2) ? string.Format(...) : string.Format(...) 
0
source share

You can write your own wrapper 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); } 
0
source share

All Articles