C
Chuck Heatherly
Hi,
I am encountering a serious problem with using String.Format(string format, params object[] args). If I try to format a
string that happens to include curly brackets that are not intended to be format specifiers, then I get this exception
thrown:
System.FormatException: Input string was not in a correct format.
at System.Text.StringBuilder.FormatError()
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
Basically, what I am doing is writing a Logger class that handles log files. I have two main methods in this class that
I route all of my output through:
public void Write(string format, params object[] args);
public void WriteLine(string format, params object[] args);
But I am hitting a problem when the format variable happens to contain curly brackets that are not used as format
specifiers (i.e., I mean an embedded string like "{one two}" as opposed to {0}, {1}, etc.)
I guess I could quit using these types of functions and just concatenate strings together, but that is a lot of extra
memory allocation, and I really like the compact format of the method with the parameter array.
Here's a Short But Complete Program(tm):
using System;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string s = String.Format("This is fine: {0}, but this is not: {some text}.", 1);
}
}
}
Thanks,
Chuck
I am encountering a serious problem with using String.Format(string format, params object[] args). If I try to format a
string that happens to include curly brackets that are not intended to be format specifiers, then I get this exception
thrown:
System.FormatException: Input string was not in a correct format.
at System.Text.StringBuilder.FormatError()
at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
Basically, what I am doing is writing a Logger class that handles log files. I have two main methods in this class that
I route all of my output through:
public void Write(string format, params object[] args);
public void WriteLine(string format, params object[] args);
But I am hitting a problem when the format variable happens to contain curly brackets that are not used as format
specifiers (i.e., I mean an embedded string like "{one two}" as opposed to {0}, {1}, etc.)
I guess I could quit using these types of functions and just concatenate strings together, but that is a lot of extra
memory allocation, and I really like the compact format of the method with the parameter array.
Here's a Short But Complete Program(tm):
using System;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string s = String.Format("This is fine: {0}, but this is not: {some text}.", 1);
}
}
}
Thanks,
Chuck