-1

I have a long string of text with very specific indents, specifically text for a VBA run.

I was wondering how to turn it into a string for declaring using this:

Clipboard.SetText(string);

I've seen people using it as a text file, adding it in resources, but I'm not sure about any of those.

phuclv
  • 37,963
  • 15
  • 156
  • 475

1 Answers1

0

Use a

string s = @"Verbatim string
    with indentation
        embedded new line
            and special characters: C:\myfolder\myfile.txt
                You can embed things like VBA source code like this
Sub Macro1()
' Macro1 Macro
    If a = b
        ' Do something
    End If
End Sub";

Console.WriteLine(s);
Clipboard.SetText(s);

Demo

When a string is prepended with @ all special escapes will be disabled. Now you can have embedded new lines and backslashes inside the string. The only downside is that you have to escape a double quote with 2 double quotes

string t = @"""C:\Windows\System32"" is the system folder"

so it's less convenient that C++ raw string literal or heredoc in many other languages, but it's still better than escaping or concatenating everything

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • I made a text file containing the VBA code and put it in the resources folder as string called ExcelVBA. I plan on exporting a desktop app and have the code be universal with anyone who uses the app, so what would the best approach if I wanted to use that without having a long string to intent on the code. – Michel Faraday Apr 02 '20 at 21:39