Escaping single quote in String.Format()
You can escape it using a backslash which you will have to escape. Either return dt.ToString(@”MMM d \’yy ‘at’ H:mmm”); or return dt.ToString(“MMM d \\’yy ‘at’ H:mmm”);
You can escape it using a backslash which you will have to escape. Either return dt.ToString(@”MMM d \’yy ‘at’ H:mmm”); or return dt.ToString(“MMM d \\’yy ‘at’ H:mmm”);
In general, you will want to use InvariantCulture if the string you are generating is to be persisted in a way that is independent of the current user’s culture (e.g. in the registry, or in a file). You will want to use CurrentCulture for strings that are to be presented in the UI to the … Read more
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
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
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
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
At a guess, the html contains javascript or another source of braces ({ and }) which would all need doubling (to {{ and }}) to be usable with string.Format. I expect a different (more obvious) token may be in order, i.e. %%FILENAME%%. Then use either regex or string.Replace. If you have a single tag, string.Replace … Read more
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
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
Python’s string format method can take a format spec. >>> “{0:b}”.format(37) ‘100101’ Format spec docs for Python 2 Format spec docs for Python 3