WPF and string formatting
Suppose I have XAML:
<Window.Resources>
<v:MyClass x:Key="whatever" Text="foo\nbar" />
</Window.Resources>
Obviously, I want a newline character in the MyClass.Text property, but the XAML parser builds an object with the literal string "foo \ nbar".
Is there (a) a way to convince the analyzer to translate the escape sequences, or (b) the .NET method to interpret the string in how the C # compiler will be?
I understand that I can find \nsequences there , but it would be better to have a general way to do this.
, \n , [...]
, , \n, - :
string s = "foo\\nbar";
s = s.Replace("\\n", "\n");
, b), , :
using System.Text.RegularExpressions;
// snip
string s = "foo\\nbar";
Regex r = new Regex("\\\\[rnt\\\\]");
s = r.Replace(s, ReplaceControlChars); ;
// /snip
string ReplaceControlChars(Match m)
{
switch (m.ToString()[1])
{
case 'r': return "\r";
case 'n': return "\n";
case '\\': return "\\";
case 't': return "\t";
// some control character we don't know how to handle
default: return m.ToString();
}
}