string.format

Regex for Matching Custom Syntax

Hello, I am trying to write a regular expression to match and split a custom variable syntax in C#. The idea here is a custom formatting of string values very similar to the .NET String.Format/{0} style of string formatting. For example the user would define a String format to be evaluated at runtime like so: D:\Path\{LanguageId}\{Pers...

What is the most readable use of String.Format for long strings with many parameters?

For instance: String login = String.Format("computer={0}&ver={1}.{2}.{3}&from={4}&realcomputername={5}&type={6}&Channels={7}&Hotkeys={8}&ID={9}\r\n", serviceConfig.Computer, serviceConfig.Version.Major, serviceConfig.Version.Minor, serviceConfig.Version.Build, userName, ...

Format decimal for percentage values?

What I want is something like this: String.Format("Value: {0:%%}.", 0.8526) Where %% is that format provider or whatever I am looking for. Should result: Value: %85.26.. I basically need it for wpf binding, but first let's solve the general formatting issue: <TextBlock Text="{Binding Percent, StringFormat=%%}" /> ...

Possible to pass format specifier for an argument as another argument to String.Format?

For instance, let's say I have the DateTime format-string in a string variable, is there any syntax or method in .NET that would let me do the equivalent of this invalid code: String line = String.Format("{0:{1}}", DateTime.Now, dateTimeFormat); ^^^ ^ ...

Is there a way to do String.Format() in javascript?

So I have a client who does not allow any server side coding, except in rare occurences classic asp, so everything is HTML and javascript. So basically I need to build a URL from the form and then redirect. Javascript isn't necessarily my thing, but this would take me 5 minutes in asp.net using String.Format. Is there a String.Forma...

Sticking HTML formatting into System.String object in C#

How do I stick HTML formatting into a String object in C#? Here's what I have: c.DepartmentAbbr.ToString() + " - (" + c.DepartmentName.ToString() + ")" where c.DepartmentAbbr.ToString() and c.DepartmentName.ToString() are both fields being selected from a data context using LINQ. Here's what I essentially want: "<b>" + c.Department...

C++ Equivalent of %ld in Java for String.format()

For instance, "%11.2lf" in C++ becomes "%11.2f" in Java. How about for long format? ...

format string- compile time checking

Is there any way to check the format string at compile time ? Example: Console.WriteLine("{0} is a really {1} site", "stackoverflow.com", "cool");//this will run //this will give an exception as only one argument is supplied Console.WriteLine("{0} is a really {1} site", "stackoverflow.com"); Exception:"Index (zero based) must b...

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

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...

what does this string.format piece of code do?

I have this piece of code in c#: private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter stwr, bool getColumnName) { for (int i = 0; i < reader.FieldCount; i++) stwr.Writeline(String.Format("<td>{0}</td"), getColumnName ? reader.GetName(i) : reader.GetValue(i).ToString())); } I'm trying to understand what t...

How do I format Int32 numbers?

What is the best way to get formatted Int32 numbers? Let say I have this o function: string o(int x); This is the value that o need to return according to x x = 0 => o = 00 x = 1 => o = 01 x = 5 => o = 05 x = 10 => o = 10 x = 31 => o = 31 x = 106 => o = 106 ...

How can I determine if a composite format string is invalid?

Per the documentation, String.Format will throw a FormatException if either (A) the format string is invalid or (B) the format string contains an index that cannot be found in the args array. I want to be able to determine which (if either) of those conditions fail on any arbitrary string and array of arguments. Is there anything that ...

Convert C# format to VB

I am sure this is a simple question for you guys but I don't know what this developer is doing. name = String.Format(MyStringBuilder + ""); If I convert this to VB I get the message "operator + is not defined for types system.text.stringbuilder and string". Same thing if I use &. ...

optimize a string.Format + replace.

I have this function. The visual studio profile marked the line with string.Format as hot and were i spend much of my time. How can i write this loop more efficiently? public string EscapeNoPredicate(string sz) { var s = new StringBuilder(sz); s.Replace(sepStr, sepStr + sepStr); foreach (char v in Illeg...

Is there a way to reduce the verbosity of using String.Format(...., p1, p2, p3)?

I often use String.Format() because it makes the building of strings more readable and manageable. Is there anyway to reduce its syntactical verbosity, e.g. with an extension method, etc.? Logger.LogEntry(String.Format("text '{0}' registered", pair.IdCode)); public static void LogEntry(string message) { ... } e.g. I would like t...

Why do the overloads of String.Format exist?

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 ...

MicrosoftAjax date.format function not working in Chrome

There is a bug in MicrosoftAjax.js String.format function when using Chrome 5.0.375. The function randomly returns 'undefined' for some dates. I went around that by using jquery date formatter that comes with datepicker. ...

String Format not working

I am trying to display a number, stored in a dataset as a string, as a phone number. I have a label that Binds to a value but doesn't display the format that I passed as an arg: <asp:Label ID="lbl005108002" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "phone number", "{0:(###) ###-####}") %>'></asp:Label> As a test I t...

Vb.NET String.Format with ArrayList

Hi, I'm trying to use an arraylist as the parameter to String.Format. msg = msg & String.Format("<td>{0}</td>" & _ "<td>{1}</td>" & _ "<td>{2}</td>" & _ "<td>{3}</td>" & _ ...

Translating C++'s sprintf format string to C#'s string.Format

I found the following C++ code (comments added myself): // frame_name is a char array // prefix is std::string // k is a for loop counter // frames is a std::vector string sprintf(frameName, "%s_%0*s.bmp", prefix.c_str(), k, frames[k].c_str()); I then try to translate it to C# // prefix is string // k is a for loop counter // frames ...