You need to do what you call it padding. This up to already exists on . NET but only left and right, you need it to do in both, so the ideal would be to create an extension method for this.
using System;
using static System.Console;
public class Program {
public static void Main() {
Write($"|{("Hello World".PadBoth(42))}|");
}
}
namespace System {
public static class StringExt {
public static string PadBoth(this string str, int length, char character = ' ') => str.PadLeft((length - str.Length) / 2 + str.Length, character).PadRight(length, character);
}
}
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
Particularly I prefer to do this way. But if you are sure (can you have?) that you will not use this again nothing prevents you from using the code inline instead of creating a method.
The extension method you can put into a library with a collection of them and use in all your applications.
Solution based in that OS response.
You could subtract 42 from the number of characters of the company name and divide the result by 2, because there, you will find how many spaces to put in the beginning and end.
– emanuelsn
I’ll do this logic, thank you
– Bruno Rodrigues