0

Is there an elegant way to make whitespace string. To be more precise, I'm looking for solution similar to following:

For an empty string I could write

string emptyString = "";

but instead I use this far more discriptive way

string emptyString = string.Empty;
Miro
  • 1,778
  • 6
  • 24
  • 42
  • 3
    “far more descriptive” what are you smoking? How isn’t `""` perfectly descriptive?! The only distinction is that `string.Empty` is six times as much code. – Konrad Rudolph Jun 27 '12 at 22:09
  • 1
    You could create one: `public const String WhiteSpace = " ";` – Tim Schmelter Jun 27 '12 at 22:11
  • 5
    While there is a great variety among witespace strings, there could possibly be only one empty string. – Sergey Kalinichenko Jun 27 '12 at 22:12
  • you can define your own constant for a space if needed. As what i know .net doesn't define it for you since a space can be a blankspace or even a tab... – trillions Jun 27 '12 at 22:13
  • 1
    @dasblinkenlight: Actually that is only true as of .NET 2.0 iirc. Before that not all instances of `""` referred to the same interned string. – Ed S. Jun 27 '12 at 22:13
  • 1
    Also, I would argue that `string.Empty` is no more descriptive than `""`. I mean really, when has `""` ever confused anyone? You can easily go down the crazy path with this stuff. – Ed S. Jun 27 '12 at 22:15
  • 1
    Following the comment from @dasblinkenlight, look at this MSDN on [Char.IsWhiteSpace](http://msdn.microsoft.com/en-us/library/t809ektx.aspx) method. As you can see, there are numerous characters that qualifies as 'whitespace'. That's one of the reasons why you need a method and not a constant. – Steve Jun 27 '12 at 22:17
  • This is very similar to this question - http://stackoverflow.com/questions/1716230/space-code-in-c-sharp – dash Jun 27 '12 at 22:17
  • 4
    I think he meant, easier / quicker to read. I find myself having to do a double take when reading code to see if it's " " or "" when I see string.Empty there is no doubt. – Orn Kristjansson Jun 27 '12 at 22:25
  • 1
    @orn: Really? I can't say I have ever had to give more than 1/16 of a nono-second worth of thought to figure out if a string constant is `" "` or `""`. Perhaps you require a larger font? – Ed S. Jun 27 '12 at 22:28
  • @EdS. lol, that might be my problem, I will try the larger font, thnx – Orn Kristjansson Jun 27 '12 at 22:31
  • I asked this because I thought if string.Empty exists why shouldn't there be string.Space or something like that... That's all. – Miro Jun 27 '12 at 22:49
  • @orn We know what he meant. I still find it ridiculous. If you do a double-take when reading `""` or `" "` may I suggest you use a mono-spaced font? There can be no confusing the two, even at a glance. No other programming language defines a constant for empty strings. Apparently those programmers have no problem reading `""` correctly. – Konrad Rudolph Jun 28 '12 at 06:52

3 Answers3

2

I find this extremely elegant:

string singleSpace = " ";

Seems perfectly expressive to me.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 1
    I do not know why you mentioned the Ruby here. Another way to do it in C# is by using String constructor: `new string(' ', 10)` or if you/he prefer `new String(' ', 10);` or yet `new String((Char)0x20, 10)` some people said that it faster -- but I haven't tested. – Jack Jun 27 '12 at 23:39
  • @Jack: eh, I don't know either, this question just made me think of it. What I did not realize is that there existed a String constructor to do that, so thanks. – Ed S. Jun 27 '12 at 23:48
  • The `StringBuilder` class provide a similar method too; just pass the number of repetitions into second parameter of `.Append()` where the first is of `char` type: `var foo = new StringBuilder().Append(' ', 10);` and then call `.ToString()` to get a `string` of 10 white-spaces. But I do not know if it is faster that a `string` constructor call, it is only another way how to do it, of course. – Jack Jun 28 '12 at 00:13
2

This is how I make single character long strings (usually for banking position-based files):

string absurdlyLongWhiteSpaceString = string.Empty.PadLeft(500);

Or '0' filled strings for empty numeric values:

string absurdlyLongZeroedString = string.Empty.PadLeft(500, '0');

Hope this helps.

1

Personally I prefer using "" over string.Empty, but there's nothing stopping you from writing a string constants class:

public static class Strings
{
    public const string Space = " ";
}

...

string whitespace = Strings.Space;

(Note that I haven't called it Whitespace, as that would be ambiguous - a tab character is whitespace, for example.)

Can't say I've ever had much use for such a class though...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194