使用string.format(不是padleft或padright)填充任意string左或右

我可以使用String.Format()填充任意字符的某个string吗?

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

我现在想要的空间是一个任意的字符。 我不能用padLeft或padRight做的原因是因为我想能够在不同的地方/时间构造格式string,然后格式化被实际执行。

– 编辑 –
看到似乎没有一个现有的解决scheme,我想到了这个问题( 之前思考编码的build议 )
–EDIT2–
我需要一些更复杂的场景,所以我select了Think Before Coding的第二个build议

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

因为代码有点太贴切了,所以我把它移到了github上:
http://gist.github.com/533905#file_padded_string_format_info

有人可以很容易地分支它什么:)

还有另一个解决scheme。

实现IFormatProvider以返回将传递给string.Format的ICustomFormatter

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

那么你可以像这样使用它:

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

您可以将string封装在实现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); } } 

然后用这样的:

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

结果:

 "->xxxxxxxxxxxxxxxHello<-" 

简单:

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" 

编辑:我误解了你的问题,我以为你在问怎么用空格填充。

你所要求的是不可能使用string.Formatalignment组件; string.Format总是用空格string.Format 。 请参阅MSDN的alignment组件部分:复合格式 。

根据Reflector,这是在StringBuilder.AppendFormat(IFormatProvider, string, object[])内部运行的代码,通过string.Format调用:

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

正如你所看到的,空白是硬编码填充空白。