[英]How to validate format for string.Format method
string.Format has following method signature string.Format具有以下方法签名
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. 我想在传递给ProcessMessage之前验证custFormat中的值以避免异常。
Let's think about this API, if it exists. 让我们考虑一下这个API,如果它存在的话。 The goal is to pre-validate a format string, to make sure String.Format
won't throw. 目标是预先验证格式字符串,以确保String.Format
不会抛出。
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 请注意,有大量不同的特殊格式模式,每种模式都具有特定类型的特定含义: 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. 虽然如果你传递的格式字符串与你的参数类型不匹配, String.Format
似乎不会抛出,但在这种情况下,格式化程序变得毫无意义。 eg String.Format("{0:0000}", "foo")
例如String.Format("{0:0000}", "foo")
-> So such an API would be truly useful only if you passed the types of the args, as well. - >因此,只有在传递了args的类型时,这样的API才真正有用。
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? 如果我们已经需要传入我们的格式字符串和类型数组(至少),那么我们基本上是String.Format
的签名,那么为什么不使用它并处理异常呢? It would be nice if something like String.TryFormat
existed, but to my knowledge it doesn't. 如果像String.TryFormat
这样的String.TryFormat
存在会很好,但据我所知它不会。
Also, pre-validating via some API, then re-validating in String.Format
itself is not ideal perf-wise. 此外,通过某些API进行预验证,然后在String.Format
重新验证本身并不理想。
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;
}
}
As long as you're only passing in 1 argument, you can look search custFormat
for {0}
. 只要您只传入1个参数,就可以查找{0}
搜索custFormat
。 If you don't find it, it's invalid. 如果找不到,则无效。
You can validate with try catch, if format throw exceptin you log information and stop treatment. 您可以使用try catch进行验证,如果格式化,则除了您记录信息并停止处理。
try
{
string.Format(custFormat, params, .., .. , ..);
}
catch(FormatException ex)
{
throw ex;
}
string message = ProcessMessage(custFormat, name);
You should use regular expressions for syntax checking and you may use some semantic checking as well. 您应该使用正则表达式进行语法检查,也可以使用一些语义检查。
Regular expression should be: (*{\\d+}*)+
正则表达式应为: (*{\\d+}*)+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.