?
Solved

C# stringbuilder appendformat

Posted on 2008-11-18
2
Medium Priority
?
1,507 Views
Last Modified: 2012-05-05
Stringbuildrer sb = New StringBuilder();
string strReportPage = "mypage.aspx";
sb.AppendFormat("{0}?",  strReportPage);
sb.AppendFormat("{0}={1}", RequestObjKeys.cSTATEABBR, this.cboStateName.SelectedValue);

What does {0} and {1} represent?
0
Comment
Question by:Faye_DBA
2 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 22985218
Hello Faye_DBA,

The represent the additional arguments specified in the function. For example:


sb.AppendFormat("{0}={1}", RequestObjKeys.cSTATEABBR, this.cboStateName.SelectedValue);

Could also be represented as:

sb.Append(RequestObjKeys.cSTATEABBR + "=" + this.cboStateName.SelectedValue);

In other words, the additional arguments are replaced into the string at the relevant point, so where {0} occurs the first additional argument (RequestObjKeys.cSTATEABBR) is placed into the resulting string and so on.

The added advantage of using the format method is that you can not only replace these placeholders easily but you can also apply formatting if appropriate. {0:yyyy-MM-dd} for example would replace the placeholder with a date value formatting it according to the supplied format yyyy-MM-dd. This can be extremely useful for complex concatenation. There are the usual caveats of course such as you have to use {{ and }} to escape the single { and } characters if used in a format function.

Regards,

TimCottee
0
 
LVL 1

Accepted Solution

by:
klace06 earned 2000 total points
ID: 22985300
Stringbuildrer sb = New StringBuilder();
string strReportPage = "mypage.aspx";
sb.AppendFormat("{0}?",  strReportPage);
sb.AppendFormat("{0}={1}", RequestObjKeys.cSTATEABBR, this.cboStateName.SelectedValue);

line 1  ---- {0}  represents  strReportPage


line 3----- {0} represents  RequestObjKeys.cSTATEABBR
{1} -----reprsents  this.cboStateName.SelectedValue




0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question