Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pad left or right with string.format (not padleft or padright) with arbitrary string

Can I use String.Format() to pad a certain string with arbitrary characters?

Console.WriteLine("->{0,18}<-", "hello"); Console.WriteLine("->{0,-18}<-", "hello");  returns   ->             hello<- ->hello             <- 

I now want the spaces to be an arbitrary character. The reason I cannot do it with padLeft or padRight is because I want to be able to construct the format string at a different place/time then the formatting is actually executed.

--EDIT--
Seen that there doesn't seem to be an existing solution to my problem I came up with this (after Think Before Coding's suggestion)
--EDIT2--
I needed some more complex scenarios so I went for Think Before Coding's second suggestion

[TestMethod] public void PaddedStringShouldPadLeft() {     string result = string.Format(new PaddedStringFormatInfo(), "->{0:20:x} {1}<-", "Hello", "World");     string expected = "->xxxxxxxxxxxxxxxHello World<-";     Assert.AreEqual(result, expected); } [TestMethod] public void PaddedStringShouldPadRight() {     string result = string.Format(new PaddedStringFormatInfo(), "->{0} {1:-20:x}<-", "Hello", "World");     string expected = "->Hello Worldxxxxxxxxxxxxxxx<-";     Assert.AreEqual(result, expected); } [TestMethod] public void ShouldPadLeftThenRight() {     string result = string.Format(new PaddedStringFormatInfo(), "->{0:10:L} {1:-10:R}<-", "Hello", "World");     string expected = "->LLLLLHello WorldRRRRR<-";     Assert.AreEqual(result, expected); } [TestMethod] public void ShouldFormatRegular() {     string result = string.Format(new PaddedStringFormatInfo(), "->{0} {1:-10}<-", "Hello", "World");     string expected = string.Format("->{0} {1,-10}<-", "Hello", "World");     Assert.AreEqual(expected, result); } 

Because the code was a bit too much to put in a post, I moved it to github as a gist:
http://gist.github.com/533905#file_padded_string_format_info

There people can easily branch it and whatever :)

like image 230
Boris Callens Avatar asked Feb 12 '09 12:02

Boris Callens


1 Answers

There is another solution.

Implement IFormatProvider to return a ICustomFormatter that will be passed to string.Format :

public class StringPadder : ICustomFormatter {   public string Format(string format, object arg,        IFormatProvider formatProvider)   {      // do padding for string arguments      // use default for others   } }  public class StringPadderFormatProvider : IFormatProvider {   public object GetFormat(Type formatType)   {       if (formatType == typeof(ICustomFormatter))         return new StringPadder();       return null;   }   public static readonly IFormatProvider Default =      new StringPadderFormatProvider(); } 

Then you can use it like this :

string.Format(StringPadderFormatProvider.Default, "->{0:x20}<-", "Hello"); 
like image 190
thinkbeforecoding Avatar answered Sep 19 '22 12:09

thinkbeforecoding