c sharp

C# String Format

C# deals with many features of strings— a string data type variable stores string in the form of characters. By doing programming, one needs to transform the format of some specified variable into another. Format() method is a built-in feature of strings declared in the string class. This method is used to insert or add the values in the variable or an object into the other string.

Example 1

For the string formatting, we will use a simple string.format() function. This function is a built-in function of string. A string can be formatted in several ways using the same function. In C and C++ programming languages, we need to include the string library to use the features of strings in the program. But in C sharp, we do not need to include any library, only the system header file is needed. This contains all information regarding the structs, classes, and etc.

Using system;

After declaring the library, we will declare the class ‘test’. Inside the class, the main function is used. An integer type variable is required to be initialized with the value. This value will be converted into the string by applying the string format functions. For applying the function, the object of the string is created. We have used this feature in such a way that we will add that integer value to a string.

String strFormat = string.Format("I am {0} years old" , age);

The {0}, parenthesis denotes the place where the number will be added. The integer value will be declared at this point. The variable is used as a parameter of this function to add the value in the string. After that through the console statement, we will display the results on the screen.

A compiler MCS is used and mono will execute the file. cs by adding the ‘.exe.’ extension to make the file executable.

$ MCS file.cs
$ mono file.exe

The results show that the complete sentence with the numeric value is embedded in the string making the whole string because the integer part is converted into the string data type.

Now, we will use two variables to add one integer and one string value in the sentence. Both the variables are used at different locations in the string. These variables will become part of the sentence after the format method is applied to them.

String strformat = string.Format("{0] eats {1}" , name, food);

We can use any location of the variable depending on the condition. While having more than one variable in a string, one thing should be kept in mind, the sequence of variables as a parameter in the function should be the same. If you mention the food first as compared to the name variable, the value of ‘food’ variable will be displayed first, as the 0 index denotes the first value. Let us see the resultant value.

You can observe that the name and food variables are replaced by the values inside them.

Example 2

The format function of the string can be used directly with the value at run time before declaring it in the variable. String formatting not only adds different values of different data types in the statement but alignment of words can also be managed through it. For example, if you want to align the input value a specific degree away from the left or right, you can use the string. format() along with the value you want to create distance. This distance is for the margin to the word. As on console, the value is displayed on the left side, so by default, if we use a positive number, the distance will be towards the right.

String strformat = string.Format("{0, 30}" , "LinuxHint");

We want to add a string that should be apart from the left side and float in the middle, so we have used 30 distances.

You can see that the string, instead of having it on the leftmost side, is displayed in the middle. This displacement is because of the figure we have mentioned. We can reduce or increase the number. The negative sign with the number will move the string in an opposite direction.

In the next example, we have taken two variables both having negative numbers for the alignment. As 0 and 1 indexes are for the first and the second value, both values will be displayed by following the order. The -20, and -40 numbers show the width of both the values.

String strformat = string.Format("{0, -20} {1, -40}" , "linuxHint", "C# program");

As both the values are negative, both are left-aligned. The first value will be aligned at the left-most position, whereas the second value will be aligned after the -40 distance from the first value.

Example 3

Following the above approach, the library and the class will contain the main program. Another way of using the string. format method is to use any mathematical operations on the variables to add or subtract the values.

Here we have used an additional operator to add two values present inside the variables. First, the string type variable will be declared with the strings that will be used for the output display later at the time of execution. The 0 index is for the first value, the 1st index shows the second value. Whereas the ‘2’ index is for the resultant value. Then two integer-type variables will contain the values.

String result = string.Format (formatstring, value1, value2, value1 + value2);

This will contain the function again itself to convert the integer type value into the string. First, both the values will be taken in the parameter, an addition operation is applied to both of them, then the answer is converted into the string through the format string function.

You can see in the execution, that the answer will be displayed along with the statement declared above through the indexes we used.

Example 4

The string formatting process also deals with date formatting. This feature is used by taking the data as a date data type itself. Then, we will apply the formatting string technique to the input data and we will get a string value. The input data is in the standard date format ‘yy/mm/dd.

String strDate = string.Format("{0: D}" , Date);

This function will take the value date as a parameter. The {0, D} will specify the first object passed.

This function will display the day name of the respective date and then the month name with the date, and then the year followed by a comma.


The string. format() function is implemented on Ubuntu operating system. A string can be formatted in several ways. The input values can be in any data type, but after the formatting process, these values are converted into a string value combined. A string. format () function is used to add the value of different data types in a sting, for the alignment purpose, for mathematical operations, and date formatting. We have explained all of these features through examples. String. Format () function formats the string irrespective of the input data type.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.