垫向左或向右的String.Format(不padleft或padright)与任意字符串
-
22-08-2019 - |
题
可否使用的String.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上的要点是:点击 http://gist.github.com/533905#file_padded_string_format_info
有人们可以轻松地分支它和什么:)
解决方案
有另一种解决方案。
实施IFormatProvider
返回将被传递到的String.format一个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);
}
可以看到,毛坯是硬编码要被填充有空白。
不隶属于 StackOverflow