Open E-School

Male
Friday, February 24, 1984

C# string.Format – Why is it important to use string.Format in your C# programs

Category: Programming Date: Wednesday, March 25, 2015

Goal

Learn the importance of using string.Format function in your C# programs rather than using normal string concatenation methods

Namespace used

System – String class is defined in the System namespace

Classes used

String

Methods used

String.Format – Static method defined in the string class.

Explanation

When working with strings you need to join them to get some meaningful results. For example you might be writing a sum function and you want to print the output of the sum of two numbers as
“The sum of 10 and 20 is 30”

Joining of two strings is called as string concatenation and the string concatenation operator for C# is + . So to join two strings in C# you will be writing the following code

String str = “Hello ” + “world ” + “ ! “;

Here we are joining three strings to form one sentence – Hello world !

String concatenation using + operator can get messy

String concatenation can seem to be a simple thing to do when the number of strings being concatenated is less. When you have large number of strings which you want to concatenate then using the + operator will mess up things. Look at the code below

String  str1,str2,str3,str4,str5,str6;    String query = “select * from table1 where col1=” + str1 +” and col2=” + str2 + “ and col3=”+ str3 + “ and col4=” + str4 + “ and col5=”+ str5 + “and col6=” + str6;

See how untidy things become when you concatenate strings using the + operator

Let’s use string.Format

Let’s now see how string.Format can make our life easier. Let’s write the same program again but using string.Format this time

String  str1,str2,str3,str4,str5,str6;    String query = string.Format(“select * from table1 where col1={0} and col2={1} and col3={2} and col4={3} and col5={4} and col6={5}”, str1, str2, str3, str4, str5, str6, );

You can very well see how organized the code becomes with the use of string.Format method. This method will replace all the place holders from {0} till {5} with the values of the variables we have given in the arguments. In the above code the string.Format method will replace {0} with str1, {1} with str2 and so on.

Conclusion

Handling of strings can be a very tough work while writing large applications. One reason is that any mistake you make in a string will directly throw a runtime error as the compiler cannot catch the errors related to strings. So it extremely important that you keep all your code related to string neat and clean and this is where the string.Format method will help you the most.

Rate this article and help us improve

Please Login to rate
Overall ratings: 0 | Rating: out of 5
Previous Article Next Article

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp