Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用string.format(不是padlift或padrright)向左或向右填充任意字符串_C#_Formatting_Padding_String.format - Fatal编程技术网

C# 用string.format(不是padlift或padrright)向左或向右填充任意字符串

C# 用string.format(不是padlift或padrright)向左或向右填充任意字符串,c#,formatting,padding,string.format,C#,Formatting,Padding,String.format,我可以使用String.Format()将某个字符串填充为任意字符吗 Console.WriteLine("->{0,18}<-", "hello"); Console.WriteLine("->{0,-18}<-", "hello"); returns -> hello<- ->hello <- Console.WriteLine(“->{0,18}{0:20:x}{1}xxxxxxxxx你

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

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

returns 

->             hello<-
->hello             <-

Console.WriteLine(“->{0,18}{0:20:x}{1}xxxxxxxxx你好世界{0}{1:-20:x}你好世界xxxxxxxxxxxxx{0:10:L}{1:-10:R}lll你好世界rrrrr{0}{1:-10}{0}{1,-10}编辑:我误解了你的问题,我以为你在问如何填充空格

使用
string.Format
alignment组件无法执行您的请求;
string.Format
始终使用空格填充。请参阅的对齐组件部分

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

如您所见,空格是硬编码的,用空格填充。

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

string.Format(“->{0:x20}xxxxxxxxxxxxx你好还有另一个解决方案

实现
IFormatProvider
以返回将传递给字符串的
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.Format(StringPadderFormatProvider.Default),“->{0:x20}Simple:



    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"


数字是可能的。如果数字而不是字符串是可能的,我会认为这真的很愚蠢。数字是怎么可能的?我有点躺在那里。但是你可以做以下操作:Console.WriteLine(“->{0:00000}我看到你的编辑;)谢谢你为我多花了一点时间来检查内部代码。请看看我在OP中的解决方案。在我看来,这个解决方案相当可靠。在Microsoft中,谁使用“num6”和“str2”编码的?请告诉我Reflector提出了变量命名。我想你应该确保字符串不是空的。它能通过测试吗:string.Fomat(“{0}”,(PaddedString)“Hello”);?这一个允许格式化多个参数。我认为GetFormat的更新的OP.parameter应该是(Type formatType)而不是(Type Type)。但是如果需要使用任意字符填充,它不起作用。我想这就是OP的要求。
string.Format(StringPadderFormatProvider.Default, "->{0:x20}<-", "Hello");


    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"