string.format

Escaping single quote in String.Format()

走远了吗. 提交于 2019-12-18 03:03:30
问题 I have been all over the 'tubes and I can't figure this one out. Might be simple. The following String.Format call: return dt.ToString("MMM d yy 'at' H:mmm"); Correctly returns this: Sep 23 08 at 12:57 Now let's say I want to add a single quote before the year, to return this: Sep 23 '08 at 12:57 Since the single quote is a reserved escape character, how do I escape the single quote to get it to display? I have tried double, triple, and quad single quotes, with no luck. 回答1: You can escape it

named String.Format, is it possible?

匆匆过客 提交于 2019-12-17 09:31:13
问题 Instead of using {0} {1} , etc. I want to use {title} instead. Then fill that data in somehow (below I used a Dictionary ). This code is invalid and throws an exception. I wanted to know if i can do something similar to what i want. Using {0 .. N} is not a problem. I was just curious. Dictionary<string, string> d = new Dictionary<string, string>(); d["a"] = "he"; d["ba"] = "llo"; d["lol"] = "world"; string a = string.Format("{a}{ba}{lol}", d); 回答1: No, but this extension method will do it

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

旧时模样 提交于 2019-12-17 08:29:09
问题 Can I use String.Format() to pad a certain string with arbitrary characters? Console.WriteLine("->{0,18}<-", "hello"); Console.WriteLine("->{0,-18}<-", "hello"); returns -> hello<- ->hello <- I now want the spaces to be an arbitrary character. The reason I cannot do it with padLeft or padRight is because I want to be able to construct the format string at a different place/time then the formatting is actually executed. --EDIT-- Seen that there doesn't seem to be an existing solution to my

JavaScript equivalent to printf/String.Format

∥☆過路亽.° 提交于 2019-12-16 18:14:24
问题 I'm looking for a good JavaScript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() ( IFormatProvider for .NET). My basic requirement is a thousand separator format for numbers for now, but something that handles lots of combinations (including dates) would be good. I realize Microsoft's Ajax library provides a version of String.Format() , but we don't want the entire overhead of that framework. 回答1: From ES6 on you could use template strings: let soMany = 10;

How can I type “{0}” more quickly in Visual Studio?

痴心易碎 提交于 2019-12-13 09:44:35
问题 In C#, it's common to have to type {0} , {1} , etc. when formatting strings with string.Format() or Console.WriteLine(). Considering its frequency, it's an awkward set of key strokes. But despite my searches, I couldn't find any sort of shorthand or hotkey to automatically insert it in Visual Studio. Has anyone figured out something to expedite the process? 回答1: You can create the following command (C#) with my Visual Commander extension to insert text and then assign a keyboard shortcut to

comma on price value is not being read

我的梦境 提交于 2019-12-13 08:25:45
问题 i have implemented a price text box wherein it shows 12,345 instead of 12345. Or sometimes. 12,345.00. However when being submitted to the database, it shows 12345. the comma is gone. any tricks on this? here is my place order button code: protected void btnPlaceOrder_Click(object sender, EventArgs e) { string productids = string.Empty; DataTable dt; if (Session["MyCart"] != null) { dt = (DataTable)Session["MyCart"]; decimal totalPrice, totalProducts; bool totalPriceConversionResult = decimal

How to get string.format to complain at compile time

孤人 提交于 2019-12-12 10:38:14
问题 The compiler has access to the format string AND the required types and parameters. So I assume there would be some way to indicate missing parameters for the varargs ... even if only for a subset of cases. Is there someway for eclipse or another ide to indicate that the varargs passed might cause a problem at runtime ? 回答1: It looks as if FindBugs can solve your problem. There are some warning categories related to format strings. http://www.google.com/search?q=%2Bjava+%2Bprintf+%2Bfindbugs

How to get String.Format not to parse {0}

烂漫一生 提交于 2019-12-12 10:02:10
问题 I am writing a code generation tool that frequently will have lines like StringBuilder sp = new Stringbuilder(); sp.AppendFormat(" public {0}TextColumn()\n", className); sp.AppendLine(" {" sp.AppendLine(" Column = new DataGridViewTextBoxColumn();"); sp.AppendFormat(" Column.DataPropertyName = \"{0}\";\n", columnName); However the issue I am having is when I run in to a line like this. sp.AppendFormat("return String.Format(\"{0} = '{0}'\", cmbList.SelectedValue);", columnName); I want the

string.Format Return value of pure method is not used

六月ゝ 毕业季﹏ 提交于 2019-12-12 02:45:51
问题 I am using razor to display decimals from my view model and then trying to format the decimals into currency: @if (Model != null && Model.Order != null) { foreach (var item in Model.Order.Where(x => x.OrderInStep2 != null)) { String.Format("{0:C}", item.OrderInStep2) } } I am getting an Return value of pure method is not used warning, but I thought it should still work. However, the formatted item is not displaying at all. It does display when I take away the formatting though. Am I missing

How can I pass a tuple to str.format()?

為{幸葍}努か 提交于 2019-12-12 02:25:08
问题 I'm trying to use the str.format() function to print a matrix in columns. This is the line that goes wrong: >>>> "{!s:4}{!s:5}".format('j',4,3) 'j 4 ' >>>> "{!s:4}{!s:5}".format(b) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: tuple index out of range >>> b ('dat', 'is') What am I doing wrong? Edit: I think I know what the problem is: I'm passing a tuple with two elements, which is than passed on to the function as a tuple with one element, my original