.NET provides you with the Insert instance method that allows you to insert strings into existing strings. For example, If you have a string “Hello World!”, you can insert the string “Happy ” in the middle to make it “Hello Happy World!”. The following code demonstrates how to use the Insert method.

string str1 = "Hello World!";
string str2 = "Happy ";
string result = str1.Insert(6, str2);

Hello Happy World!

The Insert method accepts two arguments, the first one is the position to where the string will be inserted, and the second one will be the string to be inserted. The position of the string starts with 0 and ends with length – 1. In the string “Hello World!”, we want to insert our new string right after the first space and that would be the position of W as demonstrated below.

H e l l o W o r l d !
0 1 2 3 4 5 6 7 8 9 10 11

The string “Happy ” will now be inserted in that position. Everything after that will be pushed forward. The string “Hello Happy World!” is then produced.

Adding Padding to Strings

We can also use the PadLeft and PadRight methods to add padding to the left and right of the string. For example, let say you want to add a series of space to the left of a string for alignment purposes, you can use the PadLeft method.

string str1 = "Example";
str1 = str1.PadLeft(10);


The PadLeft and PadRight methods accept an argument which will determine the padding to be inserted. As you may have notice, we passed 10 but the padding doesn’t seem to be that long. It only added 3 spaces to the left. To determine the amount of padding, we use the formula (length – padding). The length is the length of the string and the padding is the argument passed to the method. Since the string “Example” has a length of 7 and we passed 10 as the padding, then 10 – 7 = 3 which explains why only have 3 spaces inserted. If we use a padding value which is less than the length, then no padding will be added. We can also see that PadRight also follows the same formula.

string str1 = "Example";
str1 = str1.PadRight(10);

Console.WriteLine(str1 + "|");
Example   |

Note that we added the “|” character to demonstrate the padding that was added to the right of the string.

Another version of PadLeft and PadRight accepts a second argument which is the character that will be used as padding. Consider the following example.

string str1 = "Example";
str1 = str1.PadLeft(10, '*');


Adding paddings are useful when you want to output data in tabular format. Consider the following example.

string[,] data = { { "Name", "Gender", "Age" },
                   { "John", "Male", "21" },
                   { "Mark", "Male", "18" },
                   { "Jean", "Female", "18" } };
for (int i = 0; i < data.GetLength(0); i++)
    for (int j = 0; j < data.GetLength(1); j++)
        Console.Write(data[i, j].PadRight(10));

Name      Gender    Age
John      Male      21
Mark      Male      18
Jean      Female    18

We used a multidimensional string array to store our data. The first row of data is the headers. We then used nested for loops to print the data.

Written by compitionpoint

Leave a Comment

Your email address will not be published. Required fields are marked *