パッドを左または右に付文字列になります。フォーマット(ないpadleftはpadright)任意の文字列

StackOverflow https://stackoverflow.com/questions/541098

質問

利用できる文字列です。Format()をパッドにある文字列と任意の文字?

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

returns 

->             hello<-
->hello             <-

かといった空間を任意の文字です。理由はできませんでpadLeftはpadRightでこういうことができるように構築するフォーマット文字列で異なる場所/時間のフォーマットを実際に実行されます。

--編集--
が見られるように見えませんとする既存の解決は私の問題が浮かび上がったこの後 前符号化の提案)
--EDIT2--
いかに複雑なシナリオになったので 前符号化第二の提案

[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としてのgist:
http://gist.github.com/533905#file_padded_string_format_info

あの人に簡単に支店でん:)

役に立ちましたか?

解決

があります。

の実施 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");

他のヒント

きの封止、文字列、構造体を実装する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"


編集:私は誤解におうかがいったいどのようにパッド。

うお願できませんの string.Format 配列のコンポーネント string.Format 常にパッドの白文字。を参照 配向成分MSDN:複合フォーマット.

によると、反射板、このコード内 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);
}

ご覧の通り、ブランクは、ハードコードにするとと白文字。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top