Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT: A more structured approach, including all escape sequences for <code>string</code>s and <code>char</code>s. <br> Doesn't replace unicode characters with their literal equivalent. Doesn't cook eggs, either.</p> <pre><code>public class ReplaceString { static readonly IDictionary&lt;string, string&gt; m_replaceDict = new Dictionary&lt;string, string&gt;(); const string ms_regexEscapes = @"[\a\b\f\n\r\t\v\\""]"; public static string StringLiteral(string i_string) { return Regex.Replace(i_string, ms_regexEscapes, match); } public static string CharLiteral(char c) { return c == '\'' ? @"'\''" : string.Format("'{0}'", c); } private static string match(Match m) { string match = m.ToString(); if (m_replaceDict.ContainsKey(match)) { return m_replaceDict[match]; } throw new NotSupportedException(); } static ReplaceString() { m_replaceDict.Add("\a", @"\a"); m_replaceDict.Add("\b", @"\b"); m_replaceDict.Add("\f", @"\f"); m_replaceDict.Add("\n", @"\n"); m_replaceDict.Add("\r", @"\r"); m_replaceDict.Add("\t", @"\t"); m_replaceDict.Add("\v", @"\v"); m_replaceDict.Add("\\", @"\\"); m_replaceDict.Add("\0", @"\0"); //The SO parser gets fooled by the verbatim version //of the string to replace - @"\""" //so use the 'regular' version m_replaceDict.Add("\"", "\\\""); } static void Main(string[] args){ string s = "here's a \"\n\tstring\" to test"; Console.WriteLine(ReplaceString.StringLiteral(s)); Console.WriteLine(ReplaceString.CharLiteral('c')); Console.WriteLine(ReplaceString.CharLiteral('\'')); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload