• <i id='aTYzn'><tr id='aTYzn'><dt id='aTYzn'><q id='aTYzn'><span id='aTYzn'><b id='aTYzn'><form id='aTYzn'><ins id='aTYzn'></ins><ul id='aTYzn'></ul><sub id='aTYzn'></sub></form><legend id='aTYzn'></legend><bdo id='aTYzn'><pre id='aTYzn'><center id='aTYzn'></center></pre></bdo></b><th id='aTYzn'></th></span></q></dt></tr></i><div id='aTYzn'><tfoot id='aTYzn'></tfoot><dl id='aTYzn'><fieldset id='aTYzn'></fieldset></dl></div>

    <small id='aTYzn'></small><noframes id='aTYzn'>

  • <legend id='aTYzn'><style id='aTYzn'><dir id='aTYzn'><q id='aTYzn'></q></dir></style></legend>
    • <bdo id='aTYzn'></bdo><ul id='aTYzn'></ul>
    1. <tfoot id='aTYzn'></tfoot>

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

        时间:2023-05-20
          <tbody id='yBZwf'></tbody>

        <small id='yBZwf'></small><noframes id='yBZwf'>

      1. <i id='yBZwf'><tr id='yBZwf'><dt id='yBZwf'><q id='yBZwf'><span id='yBZwf'><b id='yBZwf'><form id='yBZwf'><ins id='yBZwf'></ins><ul id='yBZwf'></ul><sub id='yBZwf'></sub></form><legend id='yBZwf'></legend><bdo id='yBZwf'><pre id='yBZwf'><center id='yBZwf'></center></pre></bdo></b><th id='yBZwf'></th></span></q></dt></tr></i><div id='yBZwf'><tfoot id='yBZwf'></tfoot><dl id='yBZwf'><fieldset id='yBZwf'></fieldset></dl></div>
          • <legend id='yBZwf'><style id='yBZwf'><dir id='yBZwf'><q id='yBZwf'></q></dir></style></legend>
              <tfoot id='yBZwf'></tfoot>

                  <bdo id='yBZwf'></bdo><ul id='yBZwf'></ul>

                  本文介绍了使用任意字符串向左或向右填充 string.format(不是 padleft 或 padright)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

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

                  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             <-
                  

                  我现在希望空格是任意字符.我无法使用 padLeft 或 padRight 执行此操作的原因是因为我希望能够在不同的位置/时间构造格式字符串,然后实际执行格式化.

                  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.

                  --编辑--
                  看到我的问题似乎没有现有的解决方案,我想出了这个(在 三思而后行的建议)
                  --EDIT2--
                  我需要一些更复杂的场景,所以我选择了 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);
                  }
                  

                  因为代码有点多,不能发帖,我把它移到github上作为要点:
                  http://gist.github.com/533905#file_padded_string_format_info

                  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 :)

                  推荐答案

                  还有另一种解决方案.

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

                  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");
                  

                  这篇关于使用任意字符串向左或向右填充 string.format(不是 padleft 或 padright)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:文件大小格式提供程序 下一篇:格式化 .NET DateTime“日";没有前导零

                  相关文章

                  • <bdo id='CjZeZ'></bdo><ul id='CjZeZ'></ul>
                • <legend id='CjZeZ'><style id='CjZeZ'><dir id='CjZeZ'><q id='CjZeZ'></q></dir></style></legend>

                    <i id='CjZeZ'><tr id='CjZeZ'><dt id='CjZeZ'><q id='CjZeZ'><span id='CjZeZ'><b id='CjZeZ'><form id='CjZeZ'><ins id='CjZeZ'></ins><ul id='CjZeZ'></ul><sub id='CjZeZ'></sub></form><legend id='CjZeZ'></legend><bdo id='CjZeZ'><pre id='CjZeZ'><center id='CjZeZ'></center></pre></bdo></b><th id='CjZeZ'></th></span></q></dt></tr></i><div id='CjZeZ'><tfoot id='CjZeZ'></tfoot><dl id='CjZeZ'><fieldset id='CjZeZ'></fieldset></dl></div>
                  1. <small id='CjZeZ'></small><noframes id='CjZeZ'>

                  2. <tfoot id='CjZeZ'></tfoot>