使用string.format进行左侧或右侧填充字符串(不是padleft或padright),填充的字符串可以任意指定。

60

我能否使用String.Format()来在某个字符串中填充任意字符?

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

returns 

->             hello<-
->hello             <-

我现在希望空格成为任意字符。 我不能使用padLeft或padRight的原因是,我希望能够在不同的地方/时间构造格式字符串,然后在实际执行格式化时将其应用。

--编辑--
看到似乎没有现成的解决方案,我想出了这个解决方案(在Think Before Coding的建议之后
--编辑2--
我需要一些更复杂的情况,所以我选择了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);
}

由于代码太长,无法发表在帖子中,我将其作为gist放到了github上:
http://gist.github.com/533905#file_padded_string_format_info

这样人们就能轻松地进行分支等操作 :)


我认为你应该确保字符串不为空。它能通过测试吗: string.Format("{0}", (PaddedString)"Hello");? - configurator
4个回答

31

还有一种解决方案。

实现 IFormatProvider 接口,返回一个 ICustomFormatter 对象,将其传递给 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();
}

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

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

这个允许格式化多个参数。OP已更新。 - Boris Callens
GetFormat的参数应该是(Type formatType),而不是(Type type)。我认为。 - demokritos

12

你可以将字符串封装在实现了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<-"

3

编辑:我误解了你的问题,我以为你是在问如何使用空格填充。

你所要求的无法使用string.Format对齐组件实现;string.Format始终使用空格进行填充。请参见MSDN: Composite Formatting中的对齐组件部分。

根据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);
}

正如您所见,空格被硬编码为填充空白。


微软里是谁用“num6”和“str2”编码的?请告诉我这是反编译工具Reflector自动生成的变量名。 - Alpha
@Alpha:Reflector在发布代码中为所有局部变量提供了变量命名。 - configurator
这个解决方案既不好又很慢。 - watbywbarif
@watbywbarif:什么?这不是一个解决方案,而是一个解释为什么没有实现填充就不可能实现。 - configurator
抱歉,我的错,没有仔细阅读,也错过了慢速部分。 - watbywbarif
显示剩余4条评论

1

简单:



    dim input as string = "SPQR"
    dim format as string =""
    dim result as string = ""
'左侧填充: format = "{0,-8}" result = String.Format(format,input) 'result = "SPQR "
'右侧填充 format = "{0,8}" result = String.Format(format,input) 'result = " SPQR"

3
但是如果您需要使用任意字符进行填充,则无法正常工作。我认为这就是 OP 所问的问题。 - Sam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接