String quotes are sometimes a mess when they have to be escaped with \"
This command converts a selected literal string in a escaped string.
Code: <CommandCode language="csharp">
<Render>
Selection.Text = Selection.Text.Replace("\"", "\\\"");
</Render>
</CommandCode>
And why not escape also the slashes?
Code: <Render>
string selection = Selection.Text;
selection = selection.Replace("\\", "\\\\");
selection = selection.Replace("\"", "\\\"");
Selection.Text = selection;
</Render>
Here is the complete command template:
Code:<?xml version="1.0"?>
<Commands xmlns="http://schemas.devprojects.net/AutoCode/v3.0">
<Command name="EscapeString" priority="50">
<CommandBehavior>
<CommandLine shortcut="esc" />
<ActiveDocument extensions="*"/>
</CommandBehavior>
<CommandInfo>
<LanguageCategory>Common</LanguageCategory>
<Category>Strings</Category>
<Usage>esc</Usage>
<Description>Escape quotes in a string</Description>
<Author>AutocoDev</Author>
<HelpUrl>www.devprojects.net</HelpUrl>
</CommandInfo>
<CommandCode language="csharp">
<Render>
Selection.Text = Selection.Text.Replace("\"", "\\\"");
</Render>
</CommandCode>
</Command>
</Commands>
To use this command:
1. Select a string excluding the beginning and end quote characters
2. Press Ctrl+Enter
3. Write in the AutoCode InputBox "esc" and press enter
The selected string will be escaped.