Pad Left & Pad Right (Pad Center) String

The string has both PadLeft and PadRight . I need to fill in both left and right (central justification). Is there a standardized way to do this, or, even better, an integrated way to achieve the same goal?

+8
c #
source share
7 answers

Not that I knew. You can create an extension method if you see that you use it a lot. Assuming you want your line to end in the center, use something like the following

 public string PadBoth(string source, int length) { int spaces = length - source.Length; int padLeft = spaces/2 + source.Length; return source.PadLeft(padLeft).PadRight(length); } 

To make this an extension method, do it like this:

 namespace System { public static class StringExtensions { public static string PadBoth(this string str, int length) { int spaces = length - str.Length; int padLeft = spaces / 2 + str.Length; return str.PadLeft(padLeft).PadRight(length); } } } 

Aside, I just include my extensions in the system namespace - it is up to you what you do.

+11
source share

Here is a custom implementation that does not require string restructuring.

It also works correctly with odd numbers.

  static string PadCenter(string text, int newWidth) { const char filler = ' '; int length = text.Length; int charactersToPad = newWidth - length; if (charactersToPad < 0) throw new ArgumentException("New width must be greater than string length.", "newWidth"); int padLeft = charactersToPad/2 + charactersToPad%2; //add a space to the left if the string is an odd number int padRight = charactersToPad/2; StringBuilder resultBuilder = new StringBuilder(newWidth); for (int i = 0; i < padLeft; i++) resultBuilder.Insert(i, filler); for (int i = 0; i < length; i++) resultBuilder.Insert(i + padLeft, text[i]); for (int i = newWidth - padRight; i < newWidth; i++) resultBuilder.Insert(i, filler); return resultBuilder.ToString(); } 
+3
source share

You can do it yourself:

  string test = "Wibble"; int padTo = 12; int padSize = (padTo - test.Length) / 2; if (padSize > 0) { test = test.Trim().PadLeft(test.Length + padSize).PadRight(test.Length + 2 * padSize); } 

Just adjust it to deal with odd fill lengths as needed and make it an extension method if it makes your life easier.

+1
source share

Here's a slightly improved version of the @ david-colwell extension method, which also optionally accepts a fill character:

 namespace System { public static class StringExtensions { public static string PadSides(this string str, int totalWidth, char paddingChar = ' ') { int padding = totalWidth - str.Length; int padLeft = padding / 2 + str.Length; return str.PadLeft(padLeft, paddingChar).PadRight(totalWidth, paddingChar); } } } 
0
source share

Here is some improvement I think.

 namespace System { public static class StringExtensions { public static string PadCenter(this string str, int totalLength, char padChar = '\u00A0') { int padAmount = totalLength - str.Length; if (padAmount <= 1) { if (padAmount == 1) { return str.PadRight(totalLength); } return str; } int padLeft = padAmount/2 + str.Length; return str.PadLeft(padLeft).PadRight(totalLength); } } } 
0
source share

You can also create your extension as follows:

 public static string PadBoth(this string s, int padValue) { return s.PadLeft(padValue).PadRight(padValue); } 

and use the PadBoth method on the line.

-one
source share
  /* Output looks like this *****Luke***** *****Leia***** *****Han****** **Chewbecca*** */ string result = ""; string names = "Luke,Leia,Han,Chewbecca"; string[] charA = names.Split(','); for (int i = 0; i < charA.Length; i++) { int padLeft = (14 - charA[i].Length) / 2; string temp = charA[i].PadLeft(charA[i].Length + padLeft, '*'); result += temp.PadRight(14, '*') + "\n"; } Console.WriteLine(result); 
-one
source share

All Articles