Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty String Literal

Tags:

string

c#

I have come across some code during a code-review where a old coworker has done the following:

const string replacement = @""; 

This string is used in a regex expression as a replacement for what is matched. My question is what is the purpose of adding the @ literal sign to the beginning of an empty string. There should not be anything to literally interpret.

Would there be any difference in impact between: @""; and "";?

like image 490
FiringSquadWitness Avatar asked Jun 11 '15 22:06

FiringSquadWitness


People also ask

What is a null string literal?

{null string literal} A null string literal is a string_literal with no string_elements between the quotation marks. 7. 6 An end of line cannot appear in a string_literal. 7.1/2. 7 {AI95-00285-01} No transformation is performed on the sequence of characters of a string_literal.

What is an empty string called?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .

Is empty string falsey in C?

it returns true if the string length is 0, false otherwise.

What is empty string write an example?

In formal treatments, the empty string is denoted with ε or sometimes Λ or λ. The empty string should not be confused with the empty language ∅, which is a formal language (i.e. a set of strings) that contains no strings, not even the empty string. The empty string has several properties: |ε| = 0.


Video Answer


1 Answers

This string is used in a regex expression

Regular expressions make heavy use of the \ character. For example, the following is a regular expression to match precentages from 0 to 100 that always have four decimal places:

^(100\.0000|[1-9]?\d\.\d{4})$

Because \ has to be escaped in the more common C# syntax to \\ the @"" form allows for the regex to be more easily read, compare:

"^(100\\.0000|[1-9]?\\d\\.\\d{4})$"
@"^(100\.0000|[1-9]?\d\.\d{4})$"

And for this reason people often get into the habit of using the @"" form when they are using regular expressions, even in cases where it makes no difference. For one thing, if they later change to something where it does make a difference the only need to change the expression, not the code for the string itself.

I would suggest that this is likely why your colleague used @"" rather than "" in this particular case. The .NET produced is the same, but they are used to using @"" with regular expressions.

like image 127
Jon Hanna Avatar answered Sep 21 '22 13:09

Jon Hanna