Convert int to binary string in Python
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
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
Try this: string wmiQuery = string.Format(“SELECT CommandLine FROM Win32_Process WHERE Name LIKE ‘{0}%{1}'”, param1, param2); Adding some test info: string wmiQuery = string.Format ( “SELECT Name, ProcessID FROM Win32_Process WHERE Name LIKE ‘{0}%{1}'”, “wpf”, “.exe” ); Console.WriteLine ( “Query: {0}”, wmiQuery ); ManagementObjectSearcher searcher = new ManagementObjectSearcher ( wmiQuery ); ManagementObjectCollection retObjectCollection = searcher.Get ( … 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
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
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
Late to the party, but you can simply use strtr to “translate characters or replace substrings” <?php $hours = 2; $minutes = 24; $seconds = 35; // Option 1: Replacing %variable echo strtr( ‘Last time logged in was %hours hours, %minutes minutes, %seconds seconds ago’, [ ‘%hours’ => $hours, ‘%minutes’ => $minutes, ‘%seconds’ => $seconds … 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
Same way as with printf — use a – modifier in the format
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
Use the P format string. This will vary by culture: String.Format(“Value: {0:P2}.”, 0.8526) // formats as 85.26 % (varies by culture)