16

Using string.Empty instead of "" is really cute and make the code more clear. I'm wondering if there's a good named constant to replace " " too. I've found some ideas like using string.Empty.PadLeft(1) or string.Empty.PadRight(1), but I don't like it.

Something like string.Space to use instead of " " would be appropriate for the situation.

(Edited after comments)

To make my question's situation more clear:

In the multicultural situations, there shouldn't be any code like "Can not open the file". The string literals should be moved to a resource file and then use like Resources.CanNotOpenTheFile.

To make sure that happens, It seems a good rule, not to have any string literals in the code. So looking code at a glance, you can find bad implementations quickly. I think that's a good explanation of why I'm trying not to use " in the code.

mehrandvd
  • 8,806
  • 12
  • 64
  • 111
  • 3
    What have you got against `""` and `" "` ? personally I find them perfectly clear (especially the first one, which I **vastly** prefer to `string.Empty`) – Marc Gravell Mar 07 '13 at 11:22
  • I agree with @MarcGravell. Why type 12 characters when you can type 3.. – Simon Whitehead Mar 07 '13 at 11:23
  • Yes, "" is much more readable (and just as efficient as using String.Empty)! – Matthew Watson Mar 07 '13 at 11:24
  • @MarcGravell most of code analyzers like StyleCop or Resharper suggest not to use string literals in the code. – mehrandvd Mar 07 '13 at 11:27
  • @mehrandvd unless you understand the context of what problem they are trying to protect you from, that is an arbitrary thing to say. Your problems don't go away simply by using `const` instead - the issue is largely the same. Replacing `" "` with `MyString.Space` might give you a warm fuzzy feeling of having done something useful, but actually you're just doing "busy work". – Marc Gravell Mar 07 '13 at 11:30
  • @MarcGravell I just updated the question to clarify why I was looking for such syntactic sugar. – mehrandvd Mar 07 '13 at 11:50
  • 1
    @mehrandvd There is a **huge** difference between switching to an i18n provider for things like "Can not open the file", vs worrying about how to append spaces (indeed, in most i18n scenarios you can't compose strings like that - it simply doesn't work, since most phrases will need to be significantly re-ordered). Again, it comes back to the point I was trying to make: doing things for sane reasons. Switching to an i18n provider is sensible; worrying about `" "` vs `String.Space` ***is not***; these two things require very different approaches. – Marc Gravell Mar 07 '13 at 11:53
  • "‍" != "" when considering [ZWJ characters](https://emojipedia.org/zero-width-joiner/) – janv8000 Apr 13 '18 at 12:42

2 Answers2

4

There is none, but you can define your own.

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

Then use it like:

Console.Write("Test" + MyString.Space + "Text");

EDIT But you shouldn't, IMO (and based on comments from Marc Gravell and PaulRuane) since that will make the code less readable

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Once you got to do an extension method, maybe it would be better that the whole method could add the space directly :D – Matías Fidemraizer Mar 07 '13 at 11:23
  • @MatíasFidemraizer, I removed the extension method option, because I think its not approprite – Habib Mar 07 '13 at 11:24
  • 9
    And then you can define constants for each letter of the alphabet so you can have 'cute' code like `var a = MyString.H + MyString.e + MyString.l + MyString.l + MyString.o + MyString.Space + MyString.W + MyString.o + MyString.r + MyString.l + MyString.d + MyString.ExclamationMark;` Who *wouldn't* get a warm, fuzzy feeling working on code like this? – Paul Ruane Mar 07 '13 at 11:29
  • @Habib Yeah, of course, it was just a suggestion :) – Matías Fidemraizer Mar 07 '13 at 11:30
  • @PaulRuane, lol, yes, I should have mentioned that the above would make the code less readable :) – Habib Mar 07 '13 at 11:31
  • @PaulRuane I've just updated the question to clarify why I was looking for such syntactic sugar. – mehrandvd Mar 07 '13 at 11:47
  • 1
    I don't agree with the "you shouldn't" part, I would much rather have `var somePadding0 = new string(MyString.Space, 123);` vs `var somePadding1 = new string(" ", 123);` especially if using an editor without "Show whitespace" support – janv8000 Apr 13 '18 at 12:47
1

Use a constant char, there's really no need to use a string like Habib recommends.

public static class StringTools
{
    public const char Whitespace = ' ';
}
animaonline
  • 3,715
  • 5
  • 30
  • 57