垫向左或向右的String.Format(不padleft或padright)与任意字符串 [英] Pad left or right with string.format (not padleft or padright) with arbitrary string

查看:211
本文介绍了垫向左或向右的String.Format(不padleft或padright)与任意字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用的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.

- 编辑 - 结果
可见,似乎没有被现有的解决我的问题,我想出了这个(后<一个href=\"http://stackoverflow.com/questions/541098/pad-left-or-right-with-string-format-not-padleft-or-padright-with-arbitrary-str/541210#541210\">Think编码的建议之前)结果
- EDIT2 - 结果
我需要一些更复杂的情况,所以我去了<一个href=\"http://stackoverflow.com/questions/541098/pad-left-or-right-with-string-format-not-padleft-or-padright-with-arbitrary-str/541323#541323\">Think编码的第二个建议之前

--EDIT--
Seen that there doesn't seem to be an existing solution to my problem I came up with this (after Think Before Coding's suggestion)
--EDIT2--
I needed some more complex scenarios so I went for Think Before Coding's second suggestion

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

由于code是一个有点过分投入在后,我把它转移到GitHub的一个要点是:结果
<一href=\"http://gist.github.com/533905#file_padded_string_format_info\">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)与任意字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆