Reputation: 529
I have a string that has special characters like this example: 12345678912\rJ\u0011 because I need to have access to this special chars to configure my application. I want to display this string exactly like this in a TextBox field so everything I have tried so far results in a string where the character \u0011
tries to render with an empty character at the end: "7896104866061\rJ"
.
I made this but it doesn't work.
string result = Regex.Replace(
ToLiteral(this.Buffer),
"[^\x00-\x7Fd]",
c => string.Format("\\u{0:x4}", (int)c.Value[0]))
.Replace(@"\", @"\\");
public static string ToLiteral(string input)
{
using (var writer = new StringWriter())
{
using (var provider = CodeDomProvider.CreateProvider("CSharp"))
{
provider.GenerateCodeFromExpression(
new CodePrimitiveExpression(input),
writer,
null);
return writer.ToString();
}
}
}
What need I do? Thanks in advance.
Upvotes: 6
Views: 2698
Reputation: 186698
Let's build the string in a loop; we have several categories of characters:
\n
, \t
, \r
\u0011
"
and \
Code:
using System.Linq;
...
private static IReadOnlyDictionary<char, string> s_KnownControls = new
Dictionary<char, string>() {
{ '\0', "\\0"},
{ '\a', "\\a"},
{ '\b', "\\b"},
{ '\n', "\\n"},
{ '\v', "\\v"},
{ '\t', "\\t"},
{ '\f', "\\f"},
{ '\r', "\\r"},
};
public static string ToLiteral(string input) {
if (null == input)
return null;
StringBuilder sb = new StringBuilder();
foreach (var c in input) {
if (char.IsControl(c))
sb.Append(s_KnownControls.TryGetValue(c, out var s)
? s
: $"\\u{((int)c):x4}");
else {
if (c == '"' || c == '\\') // escapement
sb.Append('\\');
sb.Append(c);
}
}
return sb.ToString();
}
Demo:
string[] tests = new string[] {
"12345678912\rJ\u0011",
"abc\tdef\t\0A\n\a\bB",
"abc\"def\"xyz\\pqr"
};
string report = string.Join(Environment.NewLine, tests
.Select(test => ToLiteral(test)));
Outcome:
12345678912\rJ\u0011
abc\tdef\t\0A\n\a\bB
abc\"def\"xyz\\pqr
Upvotes: 1
Reputation: 175
Here my snippet to display ctrl chars: https://gist.github.com/TheTrigger/6efa6a8e42eedf1e61e0db8e9ef4360a
using System.Text;
namespace CtrlCharReplace
{
public static class Extensions
{
public static string ReplaceCtrl(this string s)
{
var sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
var isCtrl = char.IsControl(c);
var n = char.ConvertToUtf32(s, i);
if (isCtrl)
{
sb.Append($"\\u{n:X4}");
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
}
}
Upvotes: 2
Reputation:
Do the replace of \
with \\
before the call to ToLiteral
, and you will get the desired output.
string result = Regex.Replace(ToLiteral("12345678912\\rJ\\u0011".Replace(@"\", @"\\")), "[^\x00-\x7Fd]", c => string.Format("\\u{0:x4}", (int)c.Value[0]));
Upvotes: 1