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

Is it possible to use String.Format () to enter a specific string with arbitrary characters?

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

Now I want the spaces to be an arbitrary character. The reason I cannot do this with padLeft or padRight is because I want to be able to create a format string in another place / time, then formatting is actually done.

- EDIT -
It can be seen that there seems to be no existing solution to my problem, I came up with this (after Think Before Coding suggestion )
- EDIT2 -
I needed more complex scripts, so I went for the second sentence of Think Before Coding

 [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); } 

Since the code was too large to post, I moved it to github as an entity:
http://gist.github.com/533905#file_padded_string_format_info

There, people can easily branch it and that's it :)

+55
c # formatting padding string.format
Feb 12 '09 at 12:49
source share
4 answers

There is another solution.

IFormatProvider to return the ICustomFormatter to 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 as follows:

 string.Format(StringPadderFormatProvider.Default, "->{0:x20}<-", "Hello"); 
+30
Feb 12 '09 at 13:50
source share

You can encapsulate a string in a structure that implements IFormattable

 public struct PaddedString : IFormattable { private string value; public PaddedString(string value) { this.value = value; } public string ToString(string format, IFormatProvider formatProvider) { //... use the format to pad value } public static explicit operator PaddedString(string value) { return new PaddedString(value); } } 

Then use it as follows:

  string.Format("->{0:x20}<-", (PaddedString)"Hello"); 

result:

 "->xxxxxxxxxxxxxxxHello<-" 
+11
Feb 12 '09 at 13:27
source share

Plain:

 dim input as string = "SPQR" dim format as string ="" dim result as string = "" 'pad left: format = "{0,-8}" result = String.Format(format,input) 'result = "SPQR " 'pad right format = "{0,8}" result = String.Format(format,input) 'result = " SPQR"
dim input as string = "SPQR" dim format as string ="" dim result as string = "" 'pad left: format = "{0,-8}" result = String.Format(format,input) 'result = "SPQR " 'pad right format = "{0,8}" result = String.Format(format,input) 'result = " SPQR" 
+3
Oct 14 '16 at 9:12
source share

Edit: I misunderstood your question, I thought you were asking how to fill in the blanks.

What you request is not possible using the alignment component string.Format ; string.Format always string.Format spaces. See Alignment Component Section of MSDN: Composite Formatting .

According to Reflector, this is the code that works inside StringBuilder.AppendFormat(IFormatProvider, string, object[]) , which is called by string.Format :

 int repeatCount = num6 - str2.Length; if (!flag && (repeatCount > 0)) { this.Append(' ', repeatCount); } this.Append(str2); if (flag && (repeatCount > 0)) { this.Append(' ', repeatCount); } 

As you can see, spaces are hardcoded to fill in spaces.

+2
Feb 12 '09 at 13:00
source share



All Articles