James123
James123

Reputation: 11652

string double quotes replace with empty in c#

I have string.

There are no items to show in this view of the "Personal Documents"

then assign to string str variable

string str ="There are no items to show in this view 
of the \"Personal Documents\" library"

Now planning to replace "\" and make it to actual string to str object. I tried below, but did not worked

str = str.Replace(@"\",string.Empty);

I want str value should be

string str ="There are no items to show in this view 
of the "Personal Documents" library"

I need to find this string in another string. While searching into that string. I couldn't found because str contains "\".

Upvotes: 3

Views: 7816

Answers (4)

dtb
dtb

Reputation: 217233

To express the string

There are no items to show in this view of the "Personal Documents"

in C#, you need to escape the " character, because the " is used in C# to enclose string literals.

There are two options:

  • regular string literals

    string str = "There are no items to show in this view of the \"Personal Documents\"";
                                                                 ↑                   ↑
    
  • and verbatim string literals

    string str = @"There are no items to show in this view of the ""Personal Documents""";
                 ↑                                                ↑                   ↑
    

Note that in both cases the " character is escaped.

In both cases, the str variable holds the same string. For example,

Console.WriteLine(str);

prints

There are no items to show in this view of the "Personal Documents"

See also: Strings (MSDN)


EDIT: If you have the string

There are no items to show in this view of the "Personal Documents"

and want to turn in into

There are no items to show in this view of the Personal Documents

you can use the String.Replace Method like this:

string str = "There are no items to show in this view of the \"Personal Documents\"";

string result = str.Replace("\"", "");

"\"" denotes the string that consists of the single character " (which, again, is escaped in this regular string literal), and "" is the empty string.

Upvotes: 13

Anders Abel
Anders Abel

Reputation: 69250

If I understand your question correctly, you want to replace the \" of the string constant with ". That is not needed, it is done by the compiler for you.

The reason that you have to put a \ (escape sequence) before the " is to tell that compiler that you want to include the " in the string, not to terminate the string constant.

When stored in memory, the escape character is already removed and when the string is used (e.g. printed on screen) it is not shown.

E.g. the line:

Console.WriteLine("A string with a \" and a \\\" too.");

Will be printed as:

A string with a " and a \" too.

Upvotes: 3

TheDevOpsGuru
TheDevOpsGuru

Reputation: 1580

Try:

string str = @"There are no items to show in this view of the ""Personal Documents"" library"

Upvotes: 0

Andy_Vulhop
Andy_Vulhop

Reputation: 4789

string str ="There are no items to show in this view of the \"Personal Documents\" library";

this works fine.

fire up a console app and write str to console with Console.Write as a confidence boost.

Upvotes: 2

Related Questions