string.format

.NET: Is there a String.Format form for inserting the value of an object property into a string?

 ̄綄美尐妖づ 提交于 2019-12-03 09:49:54
问题 I think the direct answer to the question is 'No' but I'm hoping that someone has written a real simple library to do this (or I can do it...ugh...) Let me demonstrate what I am looking for with an example. Suppose I had the following: class Person { string Name {get; set;} int NumberOfCats {get; set;} DateTime TimeTheyWillDie {get; set;} } I would like to be able to do something like this: static void Main() { var p1 = new Person() {Name="John", NumberOfCats=0, TimeTheyWillDie=DateTime.Today

How does string.Format handle null values?

巧了我就是萌 提交于 2019-12-03 09:17:08
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? I'm guessing here, but it looks to be the difference of which overloaded call you're hitting. String.Format has multiple overloads, it's just about

String Interpolation in Visual Studio 2015 and IFormatProvider (CA1305)

感情迁移 提交于 2019-12-03 08:17:07
问题 The new string interpolation style in Visual Studio 2015 is this: Dim s = $"Hello {name}" But if I use this the code analysis tells me I break CA1305: Specify IFormatProvider In the old times I did it like this: Dim s = String.Format(Globalization.CultureInfo.InvariantCulture, "Hello {0}", name) But how can it be done with the new style? I have to mention that I'm looking for a solution for .Net 4.5.2 (for .Net 4.6 dcastro has the answer) 回答1: Microsoft has made it easier to use string

Is CultureInfo.CurrentCulture really necessary in String.Format()?

核能气质少年 提交于 2019-12-03 01:12:54
How do you think is really necessary to provide IFormatProvider in method String.Format(string, object) ? Is it better to write full variant String.Format(CultureInfo.CurrentCulture, "String is {0}", str); or just String.Format("String is {0}", str); ? In general, you will want to use InvariantCulture if the string you are generating is to be persisted in a way that is independent of the current user's culture (e.g. in the registry, or in a file). You will want to use CurrentCulture for strings that are to be presented in the UI to the current user (forms, reports). Subtle bugs can arise if

.NET: Is there a String.Format form for inserting the value of an object property into a string?

我的未来我决定 提交于 2019-12-03 00:21:34
I think the direct answer to the question is 'No' but I'm hoping that someone has written a real simple library to do this (or I can do it...ugh...) Let me demonstrate what I am looking for with an example. Suppose I had the following: class Person { string Name {get; set;} int NumberOfCats {get; set;} DateTime TimeTheyWillDie {get; set;} } I would like to be able to do something like this: static void Main() { var p1 = new Person() {Name="John", NumberOfCats=0, TimeTheyWillDie=DateTime.Today}; var p2 = new Person() {Name="Mary", NumberOfCats=50, TimeTheyWIllDie=DateTime.Max}; var str = String

String Interpolation in Visual Studio 2015 and IFormatProvider (CA1305)

ε祈祈猫儿з 提交于 2019-12-02 23:35:37
The new string interpolation style in Visual Studio 2015 is this: Dim s = $"Hello {name}" But if I use this the code analysis tells me I break CA1305: Specify IFormatProvider In the old times I did it like this: Dim s = String.Format(Globalization.CultureInfo.InvariantCulture, "Hello {0}", name) But how can it be done with the new style? I have to mention that I'm looking for a solution for .Net 4.5.2 (for .Net 4.6 dcastro has the answer ) Microsoft has made it easier to use string interpolation and comply with CA1305: Specify IFormatProvider . If you are using C# 6 or later, you have access

String.Format for C++

↘锁芯ラ 提交于 2019-12-02 17:08:09
Looking for an implementation for C++ of a function like .NET's String.Format. Obviously there is printf and it's varieties, but I'm looking for something that is positional as in: String.Format("Hi there {0}. You are {1} years old. How does it feel to be {1}?", name, age); This is needed because we're going to try and make it easier to localize our app, and giving the translators {0} and {1} to position anywhere in the sentence is much easier than giving them a %s, %d, %d which must be positioned in that order in their translation. I suppose search and replace with variable inputs (va_start,

C# String.Format optional parameters

故事扮演 提交于 2019-12-01 07:03:44
I want to use string.Format with optional parameters : public static void Main(string[] args) { // Your code goes here // Console.WriteLine(string.Format("{0} {1}", "a", "b")); Console.WriteLine(string.Format("{0} {1}", "a")); } for exemple the parameter {1} to be optional and to have a default value Can you help me to do this? It depends on what you mean by "optional parameter". If you want to automatically replace null with a default value, the easiest way to do that is to use the null coalescing operator inside the arguments: String.Format("{0} {1}", "a", someNullableVariableContainingB ??

C# String.Format optional parameters

荒凉一梦 提交于 2019-12-01 04:56:55
问题 I want to use string.Format with optional parameters : public static void Main(string[] args) { // Your code goes here // Console.WriteLine(string.Format("{0} {1}", "a", "b")); Console.WriteLine(string.Format("{0} {1}", "a")); } for exemple the parameter {1} to be optional and to have a default value Can you help me to do this? 回答1: It depends on what you mean by "optional parameter". If you want to automatically replace null with a default value, the easiest way to do that is to use the null

C# String.Format - Invalid input string

社会主义新天地 提交于 2019-12-01 00:33:58
问题 I have a MVC3 HtmlHelper extension like this: public static MvcHtmlString ShowInfoBar(this HtmlHelper helper, string message, InfoMessageType messageType) { return MvcHtmlString.Create(String.Format(@"<script type=""text/javascript"">$(document).ready(function () { gtg.core.showInfoBar('{0}', '{1}'}; );</script>", message, messageType.ToString().ToLower())); } The value of message is "The product "Product Name" was saved successfully." The value of messageType is info . It keeps saying Input