替代" "创建包含多个空格字符的字符串 [英] Alternatives to " " for creating strings containing multiple whitespace characters
问题描述
我不知道是否有在C#中创造的空间更OO的方法。
I'm wondering if there's a more OO way of creating spaces in C#.
从字面上空间code!
Literally Space Code!
我目前有标签+ =新的String();
,我不禁觉得这是有点让人想起使用
而不是的String.Empty
。
I currently have tabs += new String(" ");
and I can't help but feel that this is somewhat reminiscent of using ""
instead of String.Empty
.
我可以使用什么来创建空间,是不是,
?
What can I use to create spaces that isn't " "
?
推荐答案
您可以写
" "
而不是
new String(' ')
帮助吗?
根据你做什么,你可能想看看进入 StringBuilder.Append 超载接受一个字符和一个'重复'计数:
Depending on what you do, you might want to look into the StringBuilder.Append overload that accepts a character and a 'repeat' count:
var tabs = new StringBuilder();
tabs.Append(' ', 8);
或进入字符串
构造从性格上的一个构造一个字符串'重复'计数:
or into the string
constructor that constructs a string from a character a 'repeat' count:
var tabs = new string(' ', 8);
下面是一个enterprisey OO解决方案,以满足您的所有空间一代的需要:
Here's an enterprisey OO solution to satisfy all your space generation needs:
public abstract class SpaceFactory
{
public static readonly SpaceFactory Space = new SpaceFactoryImpl();
public static readonly SpaceFactory ZeroWidth = new ZeroWidthFactoryImpl();
protected SpaceFactory { }
public abstract char GetSpace();
public virtual string GetSpaces(int count)
{
return new string(this.GetSpace(), count);
}
private class SpaceFactoryImpl : SpaceFactory
{
public override char GetSpace()
{
return '\u0020';
}
}
private class ZeroWidthFactoryImpl : SpaceFactory
{
public override char GetSpace()
{
return '\u200B';
}
}
}
这篇关于替代" "创建包含多个空格字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!