Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a named constant like string.Space to replace " " [duplicate]

Tags:

c#

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.

like image 243
mehrandvd Avatar asked Mar 07 '13 11:03

mehrandvd


People also ask

Can a constant Be a string?

A string constant is an arbitrary sequence of characters that are enclosed in single quotation marks (' '). For example, 'This is a string'. You can embed single quotation marks in strings by typing two adjacent single quotation marks.

What is the other name of string constants?

String constants, also known as string literals, are a special type of constants which store fixed sequences of characters.

How to REPLACE specific word in string c#?

In C#, Replace() method is a string method. This method is used to replace all the specified Unicode characters or specified string from the current string object and returns a new modified string. This method can be overloaded by passing arguments to it.

How to change a string value in c#?

That is not possible. In . NET strings are immutable, which means that you can't change a string. Save this answer.


1 Answers

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

like image 56
Habib Avatar answered Oct 11 '22 12:10

Habib