简体   繁体   中英

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

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.

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 (eg printed on screen) it is not shown.

Eg the line:

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

Will be printed as:

A string with a " and a \" too.
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.

尝试:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM