string-formatting

Using variables inside strings

China☆狼群 提交于 2019-12-27 12:20:25
问题 In PHP I can do the following: $name = 'John'; $var = "Hello {$name}"; // => Hello John Is there a similar language construct in C#? I know there is String.Format(); but I want to know if it can be done without calling a function/method on the string. 回答1: In C# 6 you can use string interpolation: string name = "John"; string result = $"Hello {name}"; The syntax highlighting for this in Visual Studio makes it highly readable and all of the tokens are checked. 回答2: This functionality is not

Using variables inside strings

我的未来我决定 提交于 2019-12-27 12:20:08
问题 In PHP I can do the following: $name = 'John'; $var = "Hello {$name}"; // => Hello John Is there a similar language construct in C#? I know there is String.Format(); but I want to know if it can be done without calling a function/method on the string. 回答1: In C# 6 you can use string interpolation: string name = "John"; string result = $"Hello {name}"; The syntax highlighting for this in Visual Studio makes it highly readable and all of the tokens are checked. 回答2: This functionality is not

Why doesn't __rmod__ work properly for strings? [duplicate]

大城市里の小女人 提交于 2019-12-25 18:12:33
问题 This question already has an answer here : Is it possible to overwrite str's % behaviour using __rmod__? (1 answer) Closed 2 years ago . >>> class MyInt(int): ... def __rmod__(self, other): ... return 42 ... >>> class MyStr(str): ... def __rmod__(self, other): ... return 'wat' ... >>> 0 % MyInt() 42 >>> '%r' % MyStr() "''" Why is the int subclass able to control this BinOp from the reflected side, but str can not? This seems to contradict the documented datamodel. I was hoping to use the

StringFormat, ConverterCulture, and a textbox's decimal mark

痞子三分冷 提交于 2019-12-25 13:14:23
问题 I'd like to resolve a small quirk I've been having. It's not really a quirk but rather a behavior I'd like to change if possible. If I use a {N:2} StringFormat/ConverterCulture, a TextBox is forced into having a decimal mark always (even during the process of inputting text). I mean, you can't delete the dot or the comma at all, you must be able to figure out you have to move to the next "field" of the number in order to edit the decimal points by either clicking with the mouse there or

Gridview Trim Error in ASP.NET

99封情书 提交于 2019-12-25 08:36:52
问题 This is my reference question; String.Format Same Code Different View When i try this code; var cultureWithoutCurrencySymbol = (CultureInfo)CultureInfo.CurrentCulture.Clone(); cultureWithoutCurrencySymbol.NumberFormat.CurrencySymbol = ""; GridView1.FooterRow.Cells[9].Text = String.Format(cultureWithoutCurrencySymbol, "{0:c}", sumMV).Trim(); I gettin an error like this; How can i solve this problem? Is it something wrong in my code? 回答1: Since you want currency without symbol why don't simply

Format a double value to fit into a maximum string size

牧云@^-^@ 提交于 2019-12-25 02:42:58
问题 I need to format a double value so that it fits within a field of 13 characters. Is there a way to do this with String.Format or am I stuck with character-by-character work? Edits: (hopefully they will stay this time) With cases greater than a trillion I am to report an error. It's basically a calculator interface. My own answer: private void DisplayValue(double a_value) { String displayText = String.Format("{0:0." + "".PadRight(_maxLength, '#') + "}", a_value); if (displayText.Length >

Convert number into date using javascript

为君一笑 提交于 2019-12-25 01:55:36
问题 I have date in without "/" in text field and it is in mm/dd/yy format. We received input in this format 03292014. I want to get month,date and year from this number like 03/29/2014 var m = new Date(3292014*1000) console.log(m.toGMTString()) 回答1: You could do this : var m = '03292014'.match(/(\d\d)(\d\d)(\d\d\d\d)/); var d = new Date(m[3], m[1] - 1, m[2]); Or convert the input into a standard "YYYY-MM-DD" format : var d = new Date('03292014'.replace( /(\d\d)(\d\d)(\d\d\d\d)/, '$3-$1-$2' ));

C++ Runtime string formatting

拈花ヽ惹草 提交于 2019-12-25 01:50:05
问题 Usually I use streams for formatting stuff however in this case ?I don't know the format until runtime. I want to be able to take something like the following format string: Hello {0}! Your last login was on {1,date:dd/mm/yy}. ...and feed in the variables "Fire Lancer" and 1247859223, and end up with the following formatted string: Hello Fire Lancer! Your last login was on 17/07/09. In other languages I use there is built in support for this kind of thing, eg pythons format string method,

PowerShell concatenate output of Get-ChildItem

不想你离开。 提交于 2019-12-24 22:09:16
问题 I have a working script that searches for files with a regular expression. The script returns 2 lines per file: the parent folder naùe, and the filename (matching the regex). Get-ChildItem -Path "D:\test\" -Recurse -File | Where-Object { $_.BaseName -match '^[0-9]+$' } | ForEach-Object { $_.FullName -Split '\',-3,'SimpleMatch' } | select -last 2 | Out-File "D:\wim.txt" A certain system needs to have the output on one line, concatenated with for example \ or a similar character. How can I

C# XML Writer Format AmazonEnvelope for AU Scratchpad

99封情书 提交于 2019-12-24 21:04:58
问题 OK so I'm trying to format this XML element so that it looks like this: <AmazonEnvelope xsi:noNamespaceSchemaLocation="amzn-envelope.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> This is the code I have so far: writer.WriteAttributeString("xsi", "noNamespaceSchemaLocation", null, "amzn-envelope.xsd"); writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); Now this code outputs this XML element: <AmazonEnvelope noNamespaceSchemaLocation=