How to work with verbatim string literals?

captain power 0 Reputation points
2023-05-29T22:17:03.97+00:00

When I try to define a verbatim string literal, escaped double quotes ("") are turned to \" in the real string and I cannot use mystring.replace("\"","""") to turn \" to ".

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

4 answers

Sort by: Most helpful
  1. 2023-05-29T22:21:34.1766667+00:00

    ليس لدي اجابات

    0 comments No comments

  2. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    2023-05-30T01:14:51.21+00:00

    If I read John "Henry" Adams from a text file using

    var result = File.ReadAllText("textfile1.txt").Replace("\"", "\\\"");
    Debug.WriteLine(result);
    
    

    We get John \"Henry\" Adams

    Unsure if this is what you want, you need to provide a reproducible example.


  3. Anonymous
    2023-05-30T08:00:40.0666667+00:00

    Hi @captain power , Welcome to Microsoft Q&A.

    From your updated info, it looks like you don't need to use replace.

    You just need to use @ correctly to accomplish your needs.

    var mystring = @".....'.....""......"".....'";
    Console.WriteLine(mystring);
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  4. Lamia Elshahat Eldesoky 76 Reputation points
    2023-05-30T21:27:19.8933333+00:00

    The same answer

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.