问题
I'm wondering if there's a more OO way of creating spaces in C#.
Literally Space Code!
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 " "?
回答1:
You can write
" "
instead of
new String(' ')
Does that help?
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);
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';
}
}
}
回答2:
Now that you've clarified in comments:
In my actual code I'm doing new String(' ',numberOfSpaces) so I probably need to still use the new String part.
... the other answers so far are effectively useless :(
You could write:
const char Space = ' ';
then use
new string(Space, numberOfSpaces)
but I don't see any benefit of that over
new string(' ', numberOfSpaces)
回答3:
if the number of spaces would be changing then you could do something like this:
public static string Space(int count)
{
return "".PadLeft(count);
}
Space(2);
回答4:
There is no reason to do this. All else being equal, smaller code is better code. String.Empty and new String(' ') communicate the same thing as "" and " ", they just take more characters to do it.
Trying to make it 'more OO' just adds characters for no benefit. Object-Orientation is not an end in itself.
回答5:
Depending on how prevalent this is in your code, the StringBuilder way may be better.
StringBuilder tabs = new StringBuilder();
...
tabs.Append(" ");
You can mix in the constant too...
StringBuilder tabs = new StringBuilder();
const string SPACE = " ";
...
tabs.Append(SPACE);
回答6:
Extend string to give you a method to add space
public static string AddSpace(this String text, int size)
{
return text + new string(' ', size)
}
Awful in it's own right though.
回答7:
The more "OO" way would be to find a simpler way of solving your larger business problem. For example, the fact that you have a variable named tabs suggests to me that you are trying to roll your own column alignment code. String.Format supports that directly, e.g.
// Left-align name and status, right-align amount (formatted as currency).
writer.WriteLine("Name Status Amount");
writer.WriteLine("-------------------- ---------- ----------");
foreach(var item in items) {
writer.WriteLine(string.Format("{0,-20} {1,-10} {2,10:C}", item.Name, item.Status, item.Amount));
}
回答8:
If the language allowed for it you could add an extension property to the type String which was Space, something like:
public static class StringExt
{
public static char Space(this String s)
{
get {
return ' ';
}
}
}
but that isn't possible. I think it would be better to keep the space inside the property if it was a localizable thingy, but I think spaces are universal across all languages.
回答9:
I think you are taking OO way to far.. A simple addition of a space does not need an entire class.
tabs += new String(' ');
or
tabs += " ";
is just fine.
回答10:
You could use "\t", I think, to give you a tab character. That might make the spaceing more clear.
回答11:
StringBuilder.Insert and StringBuilder.Append allow you to create any number spaces, e.g. sb.Insert(0, " ", 20) will insert 20 spaces to the start of your sb (StringBuilder) object.
回答12:
You may create class extensions.
public static class StringExtensions
{
public static string GetSpace(this String)
{
return " ";
}
}
and you can call this.
String.GetSPace();
来源:https://stackoverflow.com/questions/1716230/alternatives-to-for-creating-strings-containing-multiple-whitespace-characte