string.format

String.Format vs “string” + “string” or StringBuilder? [duplicate]

依然范特西╮ 提交于 2019-11-27 23:14:52
问题 This question already has answers here : Closed 10 years ago . Possible Duplicates: Is String.Format as efficient as StringBuilder C# String output: format or concat? What is the performance priority and what should be the conditions to prefer each of the following: String.Format("{0}, {1}", city, state); or city + ", " + state; or StringBuilder sb = new StringBuilder(); sb.Append(city); sb.Append(", "); sb.Append(state); sb.ToString(); 回答1: Compiler will optimize as much string concat as it

vsprintf or sprintf with named arguments, or simple template parsing in PHP

我的梦境 提交于 2019-11-27 22:17:32
I'm searching for a way to use named arguments for sprintf or printf . Example: sprintf( 'Last time logged in was %hours hours, %minutes minutes, %seconds seconds ago' ,$hours,$minutes, $seconds ); or via vsprintf and an associative array. I have found some coding examples here function sprintfn ($format, array $args = array()) http://php.net/manual/de/function.sprintf.php and here function vnsprintf( $format, array $data) http://php.net/manual/de/function.vsprintf.php where people wrote their own solutions. But my question is, is there maybe an standard PHP solution out there to achieve this

How to Conditionally Format a String in .Net?

前提是你 提交于 2019-11-27 17:09:25
问题 I would like to do some condition formatting of strings. I know that you can do some conditional formatting of integers and floats as follows: Int32 i = 0; i.ToString("$#,##0.00;($#,##0.00);Zero"); The above code would result in one of three formats if the variable is positive, negative, or zero. I would like to know if there is any way to use sections on string arguments. For a concrete, but contrived example, I would be looking to replace the "if" check in the following code: string

How to construct WMI query

蓝咒 提交于 2019-11-27 15:09:22
I'd like to find results that Name starts with param1, and ends with param2 but my code doesn't work string wmiQuery = string.Format("SELECT CommandLine FROM Win32_Process WHERE Name LIKE '{0}%' AND Name LIKE '%{1}'", param1, param2); ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery); ManagementObjectCollection retObjectCollection = searcher.Get(); What's wrong? For comparision string wmiQuery = string.Format("SELECT CommandLine FROM Win32_Process WHERE Name LIKE '{0}%'", param1); works well Try this: string wmiQuery = string.Format("SELECT CommandLine FROM Win32

String.Format exception when format string contains “{”

流过昼夜 提交于 2019-11-27 13:54:21
I am using VSTS 2008 + C# + .Net 2.0. When executing the following statement, there is FormatException thrown from String.Format statement, any ideas what is wrong? Here is where to get the template.html I am using. I want to format this part m={0} in template.html. string template = String.Empty; using (StreamReader textFile = new StreamReader("template.html")) { template = textFile.ReadToEnd(); String.Format(template, "video.wmv"); } http://www.mediafire.com/download.php?u4myvhbmmzg EDIT 1: Here is the content for my template.html, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/

Currency Formatting MVC

旧城冷巷雨未停 提交于 2019-11-27 13:53:41
I'm trying to format an Html.EditorFor textbox to have currency formatting, I am trying to base it off of this thread String.Format for currency on a TextBoxFor . However, my text just still shows up as 0.00 with no currency formatting. <div class="editor-field"> @Html.EditorFor(model => model.Project.GoalAmount, new { @class = "editor- field", Value = String.Format("{0:C}", Model.Project.GoalAmount) }) There is the code for what I am doing, and here is the html for that field in the website itself contained within the editor-field div of course. <input class="text-box single-line valid" data

named String.Format, is it possible?

一个人想着一个人 提交于 2019-11-27 07:35:26
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); No, but this extension method will do it static string FormatFromDictionary(this string formatString, Dictionary<string, string> ValueDict) { int i = 0;

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

被刻印的时光 ゝ 提交于 2019-11-27 06:39:46
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 problem I came up with this (after Think Before Coding's suggestion ) --EDIT2-- I needed some more complex

Why do the overloads of String.Format exist?

拈花ヽ惹草 提交于 2019-11-27 06:37:32
问题 I was using Reflector to look at the implementation of String.Format and had always been under the impression that the overloads of String.Format that took 1, 2 & 3 arguments were optimized versions of the method that takes an object array. However, what I found was that internally they create an object array and then call a method that takes an object array. 1 arg public static string Format(string format, object arg0) { if (format == null) { throw new ArgumentNullException("format"); }

How can I left-align strings using String.format()?

[亡魂溺海] 提交于 2019-11-27 01:32:48
问题 I'm using String.format() in Java trying to emulate the printf() control channel available in C. I understand how to specify that a string should be placed in a field which takes 20 characters, 5, 2 ... with 3 decimals, 2, etc. However, the strings are printed right-aligned in their field. How do I left-align the strings? Here's an example of a possible output which I would like to modify to left-align EXECUTING and CREATED in their fields. Process PID: 25 Status: ----------- EXECUTING