Cinchoo – String.Indent(), Extension Method

In this section, I’ll talk about Indent extension method. This method used to indent a string with the padding characters. Please see the below samples on how to use it.

1. Add reference to Cinchoo.Core.ExtensionMethods.dll assembly

2. Namespace System


static void Main(string[] args)
    string msg = "Hello World!{0}Welcome to".FormatString(Environment.NewLine);

    Console.WriteLine("Before Indent:");

    Console.WriteLine("After Indent:");

When you run the above code, the output will be

Before Indent:
Hello World!
Welcome to

After Indent:
        Hello World!
        Welcome to

Press any key to continue . . .

Indent() method has several overloads, they are

//Indent with 1 tab char
//Indent with 'totalWidth' number of tab chars
Indent(int totalWidth);
//Indent with 'totalWidth' number of paddingChars
Indent(int totalWidth, char paddingChar);

PS: totalWidth should be positive. In case if you pass negative value, this routine will invoke Unindent() method to remove any leading pad characters from each line of input text.

Happy coding!!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s