string.format

Is String.Format as efficient as StringBuilder

Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new StringBuilder(); string cat = "cat"; sb.Append("the ").Append(cat).(" in the hat"); string s = sb.ToString(); would that be as efficient or any more efficient as having: string cat = "cat"; string s = String.Format("The {0} in the hat", cat); If so, why? E...

C# String output: format or concat?

Let's say that you want to output or concat strings, what style do you prefer: var p = new { FirstName = "Bill", LastName = "Gates" }; Console.WriteLine("{0} {1}", p.FirstName, p.LastName); Console.WriteLine(p.FirstName + " " + p.LastName); Do you rather use format or do you simply concat strings? What is you...

escaping formatting characters in java String.format

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

String.Format or Not?

Duplicate from : http://stackoverflow.com/questions/16432/c-string-output-format-or-concat 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 ...

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

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

String.Format for C++

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

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

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

Need a custom currency format to use with String.Format

I'm trying to use String.Format("{0:c}", somevalue) in C# but am having a hard time figuring out how to configure the output to meet my needs. Here are my needs: 0 outputs to blank 1.00 outputs to $1.00 10.00 outputs to $10.00 100.00 outputs to $100.00 1000.00 outputs to $1,000.00 I've tried String.Format("{0:c}", somevalue) but for ...

Javascript printf/string.format

I'm looking for a good Javascript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() (IFormatProvider for .NET). My basic requirement is thousand seperator format for numbers for now, but something that handles lots of combinations (including dates) would be good. I realise Microsoft's AJAX library provides a ...

How do you validate a composite format string in C# against its target argument types?

Given a composite format string provided by the user (for use with String.Format) and a set of types representing the arguments that would be used to format the composite format string, how can you check that the user-provided value is valid? It should be pretty easy to create a regular expression to check that the general syntax of the...

String.Format Doesn't Format a String

It seems as though String.Format won't format a string as an input. Am I doing something wrong, or is this just native behavior? Input : 0.37 This doesn't work. string x = String.Format("{0:P}", myString) Output : 0.37 This does. string x = String.Format("{0:P}", Convert.ToDecimal(myString)) Output : 37.00 % ...

String.Format vs "string" + "string" or StringBuilder?

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

Is it better practice to use String.format over string Concatenation in Java?

Is there a perceptable difference between using String.Format and string concatenation in Java? I tend to use String.format but occasionally will slip and use a concat, I was wondering if one was better than the other. The way I see it String.Format gives you more power in "formatting" the string and concatenation means you don't have ...

String.Format("{0:C2}", -1234) (Currency format) treats negative numbers as positive

Hi I am using String.Format("{0:C2}", -1234) to format numbers. is always formats the amount to a positive number, while I want it to become $-1234 ...

named String.Format, is it possible? C#

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

String.Format exception when format string contains "{"

Hello everyone, 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 (Stream...

(ASP.NET) Can you do a String.Format on a web.config key/value?

This might be a god awful question but I'm not sure why it won't let me do this. I have a URL I need to store in Web.config, which has a dynamic parameter pulled from the web page. So I want to store: <add key="TestURL" value="https://test/subscribe?msisdn={0}&amp;code=1&amp;pass=2"/&gt; It doesn't let me do this. After th...

What might be causing String.Format to move characters around unexpectedly?

So I have this code: /* variables already initialized: int numFlips int numAggrFlips double pctAggrFlips */ String flipsMessage = String.Format( "Flips: {0} / Aggr: {1} ({2})", numFlips, numAggrFlips, pctAggrFlips.ToString("0.0%") ); For some mysterious reason, the output ends up being the following string: (Flip...

How to validate format string

We have lots of strings in our resource files that contains format .e.g “{0} has moved to {1}” These strings are passed to String.Format() by the applications, sometimes the translators mess up the “formatting markers” Therefore I wish to find/write a tool that checks that all strings in the resource file has a valid format. ...

Escaping single quote in String.Format()

I have been all over the 'tubes and I can't figure this one out. Might be simple. The following String.Format call: return dt.ToString("MMM d yy 'at' H:mmm"); Correctly returns this: Sep 23 08 at 12:57 Now let's say I want to add a single quote before the year, to return this: Sep 23 '08 at 12:57 Since the single quote is...