Performance between String.format and StringBuilder

What is “better” solely depends on your requirements: For instance String Builder will be faster, but the code will be much more unreadable, and and it would be easier to make a mistake. On the other hand String.format() produces more readable code at the cost of performance. JMH benchmark to illustrate the performance difference (notice … Read more

Pad left or right with string.format (not padleft or padright) with arbitrary string

There is another solution. Implement IFormatProvider to return a ICustomFormatter that will be passed to string.Format : public class StringPadder : ICustomFormatter { public string Format(string format, object arg, IFormatProvider formatProvider) { // do padding for string arguments // use default for others } } public class StringPadderFormatProvider : IFormatProvider { public object GetFormat(Type formatType) … Read more

How does string.Format handle null values?

I’m guessing here, but it looks to be the difference of which overloaded call you’re hitting; String.Format has multiple. In the first example, it would make sense you’re hitting String.Format(string,object). In the second example by providing null you’re most likely hitting String.Format(string,params object[]) which, per the documentation, would raise an ArgumentNullException when: format or args … Read more

Is String.Format as efficient as StringBuilder

NOTE: This answer was written when .NET 2.0 was the current version. This may no longer apply to later versions. String.Format uses a StringBuilder internally: public static string Format(IFormatProvider provider, string format, params object[] args) { if ((format == null) || (args == null)) { throw new ArgumentNullException((format == null) ? “format” : “args”); } … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)