What is the difference between MessageFormat.format and String.format in JDK 1.5?

Put simply, the main difference is in format string: MessageFormat.format() format string accepts argument positions (eg. {0}, {1}). Example: “This is year {0}!” The developer doesn’t have to worry about argument types, because they are, most often, recognized and formated according to current Locale. String.format() format string accepts argument type specifiers (eg. %d for numbers, … Read more

Logging variable data with new format string

EDIT: take a look at the StyleAdapter approach in @Dunes’ answer unlike this answer; it allows to use alternative formatting styles without the boilerplate while calling logger’s methods (debug(), info(), error(), etc). From the docs — Use of alternative formatting styles: Logging calls (logger.debug(), logger.info() etc.) only take positional parameters for the actual logging message … Read more

Why does this assert throw a format exception when comparing structures?

I’ve got it. And yes, it’s a bug. The problem is that there are two levels of string.Format going on here. The first level of formatting is something like: string template = string.Format(“Expected: {0}; Actual: {1}; Message: {2}”, expected, actual, message); Then we use string.Format with the parameters you’ve supplied: string finalMessage = string.Format(template, parameters); … Read more

How to format number of decimal places in wpf using style/template?

You should use the StringFormat on the Binding. You can use either standard string formats, or custom string formats: <TextBox Text=”{Binding Value, StringFormat=N2}” /> <TextBox Text=”{Binding Value, StringFormat={}{0:#,#.00}}” /> Note that the StringFormat only works when the target property is of type string. If you are trying to set something like a Content property (typeof(object)), … Read more

How to format TimeSpan in XAML

The format string is intended to work on a DateTime, not a TimeSpan. You could change your code to work with DateTime.Now instead. Your xaml is fine: <TextBlock Text=”{Binding MyTime,StringFormat=HH:mm}”/> Update And from .Net 4 format a TimeSpan as follows: <TextBlock Text=”{Binding MyTime,StringFormat=hh\\:mm}”/>

Warning: “format not a string literal and no format arguments”

Xcode is complaining because this is a security problem. Here’s code similar to yours: NSString *nameFormat = @”%@ %@”; NSString *firstName = @”Jon”; NSString *lastName = @”Hess %@”; NSString *name = [NSString stringWithFormat:nameFormat, firstName, lastName]; NSLog(name); That last NSLog statement is going to be executing the equivalent of this: NSLog(@”Jon Hess %@”); That’s going to … Read more