Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate format for string.Format method

string.Format has following method signature

string.Format(format, params, .., .. , ..);

I want to pass custom format each time like

string custFormat = "Hi {0} ... {n} ";   // I only care about numbers here, and want avoid  {abdb}
string name = "Foo";

string message = ProcessMessage(custFormat, name);

public string ProcessMessage(custFormat, name)
{
   return string.Format(custFormat, name);
}

I want to validate the value in custFormat before passing to ProcessMessage to avoid exception.

like image 903
codeSetter Avatar asked Aug 24 '12 15:08

codeSetter


People also ask

How do I know the format of a string?

The CHKFMT function checks a character string for incorrect characters or character types. It compares each character string to a second string, called a mask, by comparing each character in the first string to the corresponding character in the mask.

How do you check if a string is in the correct format Java?

Create a list of Formatter objects (one for each allowed pattern). Iterate that list; and try if you can parse your date string using each formatter (with lenient set to false!). If you get one that doesn't throw an exception, you know that the incoming string conforms to a valid format.

What is formatting with the format () method?

The format() method formats the specified value(s) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}. Read more about the placeholders in the Placeholder section below. The format() method returns the formatted string.


1 Answers

Let's think about this API, if it exists. The goal is to pre-validate a format string, to make sure String.Format won't throw.

Note that any string which doesn't contain a valid format slot is a valid format string - if you don't try to insert any replacements.

-> So we would need to pass in the number or args we expect to replace

Note that there are tons of different specialty formatting patterns, each with a specific meaning for specific types: http://msdn.microsoft.com/en-us/library/system.string.format.aspx

Although it seems that String.Format won't throw if you pass a format string which doesn't match your argument type, the formatter becomes meaningless in such cases. e.g. String.Format("{0:0000}", "foo")

-> So such an API would be truly useful only if you passed the types of the args, as well.

If we already need to pass in our format string and an array of types (at least), then we are basically at the signature of String.Format, so why not just use that and handle the exception? It would be nice if something like String.TryFormat existed, but to my knowledge it doesn't.

Also, pre-validating via some API, then re-validating in String.Format itself is not ideal perf-wise.

I think the cleanest solution might be to define a wrapper:

public static bool TryFormat(string format, out string result, params Object[] args)
{
   try
   {
      result = String.Format(format, args);
      return true;
   }
   catch(FormatException)
   {
      return false;
   }
}
like image 98
latkin Avatar answered Oct 06 '22 23:10

latkin