0

i want to remove all control characters from 0x00 to 0x1F.

I thought i would do it like that:

string x = "\x1BTEST";

for (string stringHex = "00"; !stringHex.Equals("1F"); stringHex = (int.Parse(stringHex, System.Globalization.NumberStyles.HexNumber) + 1).ToString("X2"))
{
    x = x.Replace("\\x" + stringHex, "");
}

The double escape blows it. Ok. But how to do it?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
tuxmania
  • 906
  • 2
  • 9
  • 28
  • 1
    Possible duplicate of [Removing hidden characters from within strings](http://stackoverflow.com/questions/15259275/removing-hidden-characters-from-within-strings) – MethodMan May 08 '17 at 14:47

2 Answers2

4

You could use LINQ:

x = new string(x.Where(c => (int)c >= 0x1F).ToArray());
itsme86
  • 19,266
  • 4
  • 41
  • 57
  • I love LINQ. However I struggle so much to apply it since i learned C# 1.0 and 2.0 and just think in old fashioned way of programming. But thanks for the cool solution. – tuxmania May 08 '17 at 15:36
1

This way you can remove control characters:

string input = "\u002Some Text";
string output = new string(input.Where(c => !char.IsControl(c)).ToArray());