String Interpolation in Visual Studio 2015 and IFormatProvider (CA1305)

You’d use the System.FormattableString or System.IFormattable class: IFormattable ifs = (IFormattable)$”Hello, {name}”; System.FormattableString fss = $”Hello, {name}”; // pass null to use the format as it was used upon initialization above. string ifresult = ifs.ToString(null, CultureInfo.InvariantCulture); string fsresult = fss.ToString(CultureInfo.InvariantCulture); You need to be compiling against Framework 4.6, as the IFormattable and FormattableString are classes … Read more

How to Conditionally Format a String in .Net?

Well, you can simplify it a bit with the conditional operator: string formatString = items.Count > 0 ? “Items: {0}; Values: {1}” : “Values: {1}”; return string.Format(formatString, itemList, valueList); Or even include it in the same statement: return string.Format(items.Count > 0 ? “Items: {0}; Values: {1}” : “Values: {1}”, itemList, valueList); Is that what you’re … Read more

named String.Format, is it possible?

No, but this extension method will do it static string FormatFromDictionary(this string formatString, Dictionary<string, string> valueDict) { int i = 0; StringBuilder newFormatString = new StringBuilder(formatString); Dictionary<string, int> keyToInt = new Dictionary<string,int>(); foreach (var tuple in valueDict) { newFormatString = newFormatString.Replace(“{” + tuple.Key + “}”, “{” + i.ToString() + “}”); keyToInt.Add(tuple.Key, i); i++; } return … Read more

Currency Formatting MVC

You could decorate your GoalAmount view model property with the [DisplayFormat] attribute: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = “{0:c}”)] public decimal GoalAmount { get; set; } and in the view simply: @Html.EditorFor(model => model.Project.GoalAmount) The second argument of the EditorFor helper doesn’t do at all what you think it does. It allows you to pass additional … Read more

String.Format: Input string was not in a correct format [closed]

Escape the “{“, “}” (by duplicating them) in the format string: “{{ cmd: \”save magellan deal\”, data: {{ id: {0} , AId: {1}, ” + “CId: {2}, CCId:{3}, LA: \”{4}\”, BA: \”{5}\” , ” + “LSA: \”{6}\” , BSA: \”{7}\” , \”path: \”{8}\”,” + “dscp: \”{9}\”, SI: \”{10}\”, CD: \”{11}\”, ” + “period: \”{12}\”, IsStatic: … Read more

Can I format NULL values in string.Format?

You can define a custom formatter that returns “NULL” if the value is null and otherwise the default formatted string, e.g.: foreach (var value in new[] { 123456.78m, -123456.78m, 0m, (decimal?)null }) { string result = string.Format( new NullFormat(), “${0:#,000.00;(#,000.00);ZERO}”, value); Console.WriteLine(result); } Output: $123.456,78 $(123.456,78) $ZERO $NULL Custom Formatter: public class NullFormat : IFormatProvider, … Read more

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