tags:

views:

122

answers:

4

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 "\".

+2  A: 
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.

Andy_Vulhop
+12  A: 

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.

dtb
A: 

Try:

string str = @"There are no items to show in this view of the ""Personal Documents"" library"
AndHeCodedIt
dtb beat me to it =)
AndHeCodedIt
The code above is not correct. The compiler will return an error. The quotation mark characters still need to be escaped with an additional quotation mark even when using a verbatim string literal (i.e. prefixing the quoted string with the @ character).
Adam Porad
I noticed this and fixed it almost immediately after I posted my answer.
AndHeCodedIt
+3  A: 

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.
Anders Abel