string.format

Problem with formatting a string with String.Format in C#

独自空忆成欢 提交于 2019-12-05 22:32:53
I need to print a string in a message box in specific format for which i am using code similar to as shown below: string text=""; for (int i=0; i<n; i++) { a=.. b=.. c=.. text += String.Format("{0, -8} {1,-4} {2,8}", a, b, c); } MessageBox.Show(text); So for following set of values: XYZ,ABC,100 X,ABC,100 I get following output: XYZ ABC 100 X ABC 100 So you can see the second line is not well formatted. Probably this is happening because i am printing this in MessageBox. The space a character and a 'space' takes is different. Any solution for this? Bob Black Try using a \t to insert tabs

How to validate format for string.Format method

坚强是说给别人听的谎言 提交于 2019-12-05 19:36:47
问题 string.Format has following method signature string.Format(format, params, .., .. , ..); I want to pass custom format each time like string custFormat = "Hi {0} ... {n} "; // I only care about numbers here, and want avoid {abdb} string name = "Foo"; string message = ProcessMessage(custFormat, name); public string ProcessMessage(custFormat, name) { return string.Format(custFormat, name); } I want to validate the value in custFormat before passing to ProcessMessage to avoid exception. 回答1: Let

escaping formatting characters in java String.format

a 夏天 提交于 2019-12-05 09:20:24
问题 This question is pretty much the same as this .Net question exept for java. How do you escape the %1$ characters in a java string.format? THe reason I need to do this is that I'm building up a string that will later have more info inserted into it. I've thought of having one of the args just be "%1$" but that doesn't seem to be very elegant? sorry if this is obvious my java is a tad rusty. 回答1: You can just double up the % 回答2: Either you can use the proposal of Draemon, either you can also

String.Format or Not? [duplicate]

岁酱吖の 提交于 2019-12-05 07:54:10
This question already has answers here : Closed 10 years ago . Duplicate from : String output: format or concat in C#? Especially in C# world using String.Format for everything is really common, normally as VB.NET developer unless I have to* I don't String.Format, I prefer normal string concatenation, such as: V1 = V2 & "test-x" & V3 & "-;" to me it's better than this: V1 = String.Format("{0} test-x {1} -;", V2, V3) Am I missing something? Or is this just a personal preference? Reasons to Use String.Format (From The Answers) ( I'll try to keep this up to date ) Localization is so much easier

Interpreting dates: Console.Writeline vs. string.Format

為{幸葍}努か 提交于 2019-12-05 07:24:19
Given the following C# code: var dt = DateTime.Now; Console.WriteLine("{0:MM/dd/yy} ... {1}", dt, string.Format("{0:MM/dd/yy}", dt)); ... when the short date (under Windows 7, Control Panel -> Region and Language -> Additonal Settings -> Date ) is set to the USA standard of " M/d/yyyy ," I get this: 06/17/14 ... 06/17/14 However, when I change the short date to " ddd dd MMM yyyy ," I get this: 06/17/14 ... 06 17 14 I was under the impression that Console.WriteLine and string.Format always string formatted DateTime values identically. What is the explanation for this discrepancy? EDIT: Looks

Using .NET string formatting, how do I format a string to display blank (empty string) for zero (0)?

大兔子大兔子 提交于 2019-12-05 06:18:41
I am using a DataBinder.Eval expression in an ASP.NET Datagrid, but I think this question applies to String formatting in .NET in general. The customer has requested that if the value of a string is 0, it should not be displayed. I have the following hack to accomplish this: <%# IIf(DataBinder.Eval(Container.DataItem, "MSDWhole").Trim = "0", "", DataBinder.Eval(Container.DataItem, "MSDWhole", "{0:N0}")) %> I would like to change the {0:N0} formatting expression so that I can eliminate the IIf statement, but can't find anything that works. You need to use the section separator , like this: <%#

How does string.Format handle null values?

微笑、不失礼 提交于 2019-12-04 14:54:49
问题 In the following code below, why do the two string.Format calls not behave the same way? In the first one, no exception is thrown, but in the second one an ArgumentNullException is thrown. static void Main(string[] args) { Exception e = null; string msgOne = string.Format("An exception occurred: {0}", e); string msgTwo = string.Format("Another exception occurred: {0}", null); } Could someone please help me understand the difference between the two? 回答1: I'm guessing here, but it looks to be

coffeescript version of string.format, sprintf() etc. for javascript or node.js

蓝咒 提交于 2019-12-04 03:53:08
How do I string.format() or sprintf() in coffeescript? Peter Lyons So there's 2 things going on here. First is interpolation, which coffeescript directly supports using double-quoted string literals and ruby style syntax like this: "The #{speed} #{color} #{animal} jumped over the lazy dog" That will replace the placeholders with the corresponding variables from the local scope. That's the idiomatic way to handle string interpolation in coffeescript (and ruby). Second is the formatting, which you should probably handle separately if you want to get numbers with specific decimal places,

String.Format vs ToString()

女生的网名这么多〃 提交于 2019-12-03 23:53:55
Can anyone explain if there is any benefit in either one of the following methods: decimal d = 12.0m; // 1. how I'd have done it myLabel.Text = d.ToString(); // 2. how I saw someone do it today myLabel.Text = String.Format("{0}", d); Just to clarify, I'm not querying what the methods do, I'm obviously happy with that, just if there is perhaps a performance benefit in one over the other in this specific example. I'm aware of the added flexibility of cultures and formatting offered by string.format(), but I'd always just 'tostring()' numerics to attach their value to a label, or text based

String.Format: Input string was not in a correct format [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-03 10:24:59
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center . The following code keep giving me error saying Input string was not in a correct format, but I am pretty sure it is right, isn't it? int id = 112; String[] newData = { "1", "2", "21", "reidb", "reidb", "reidb", "reidb", "aa", "Some description", "11", "2012-02-28", "2012-01