6

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.

2
  • 1
    So you want to convert: crtlchar to literally \u0011?
    – Fabio
    Jun 5, 2019 at 11:46
  • 1
    Yes @Fabio! This is exactly what I want to do.
    – regisls
    Jun 5, 2019 at 11:48

3 Answers 3

2

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();
        }
    }
}
0
1

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]));
1

Let's build the string in a loop; we have several categories of characters:

  1. Known controls like \n, \t, \r
  2. Controls like \u0011
  3. Characters which should be escaped: " and \
  4. Plain charactes which should be put as they are

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.