Skip to content

Instantly share code, notes, and snippets.

@TheTrigger
Last active June 5, 2019 12:09
Show Gist options
  • Save TheTrigger/6efa6a8e42eedf1e61e0db8e9ef4360a to your computer and use it in GitHub Desktop.
Save TheTrigger/6efa6a8e42eedf1e61e0db8e9ef4360a to your computer and use it in GitHub Desktop.
C# snippet to display ctrl chars
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment