I have to clear hex characters from exception message in a better way. For now it works replacing characters manually which seems total disaster like this :
var clearedStr = str.Replace(Convert.ToString((char)0x01), "")
.Replace(Convert.ToString((char)0x02), "")
.Replace(Convert.ToString((char)0x03), "")
.Replace(Convert.ToString((char)0x04), "")
.Replace(Convert.ToString((char)0x05), "")
.Replace(Convert.ToString((char)0x06), "")
.Replace(Convert.ToString((char)0x07), "")
.Replace(Convert.ToString((char)0x08), "")
.Replace(Convert.ToString((char)0x0B), "")
.Replace(Convert.ToString((char)0x0C), "")
.Replace(Convert.ToString((char)0x0E), "")
.Replace(Convert.ToString((char)0x0F), "")
.Replace(Convert.ToString((char)0x10), "")
.Replace(Convert.ToString((char)0x11), "")
.Replace(Convert.ToString((char)0x12), "")
.Replace(Convert.ToString((char)0x13), "")
.Replace(Convert.ToString((char)0x14), "")
.Replace(Convert.ToString((char)0x15), "")
.Replace(Convert.ToString((char)0x16), "")
.Replace(Convert.ToString((char)0x17), "")
.Replace(Convert.ToString((char)0x18), "")
.Replace(Convert.ToString((char)0x19), "")
.Replace(Convert.ToString((char)0x1a), "")
.Replace(Convert.ToString((char)0x1b), "")
.Replace(Convert.ToString((char)0x1c), "")
.Replace(Convert.ToString((char)0x1d), "")
.Replace(Convert.ToString((char)0x1e), "")
.Replace(Convert.ToString((char)0x84), "")
.Replace(Convert.ToString((char)0x86), "")
.Replace(Convert.ToString((char)0x87), "")
.Replace(Convert.ToString((char)0x88), "")
.Replace(Convert.ToString((char)0x89), "");
The message for example like this with hex characters :
Actually I wrote a regex but it works for hex character like 0x1e, but not for its equivalent :
But i need to find these characters, not hex equivalent :
“”,”‘”,”ƒ”,””,””,”’”,””,”š”,”ˆ”,”‰”,”Š”,”‹”,”Œ”,””,”„”, “†”, “‡”
Same characters with their symbols :
“RS: , PU1 : ‘, NBH : ƒ, US : , ESC : , PU2: ’, GS : ,
SCI: š, HTS: ˆ, HTJ : ‰, VTS : Š, PLD : ‹, PLU: Œ, SUB :, IND: „, SSA: †, ESA : ‡”
The regex is that I wrote :
http://regexstorm.net/tester?p=%5b0-9%5dx%5b0-9A-F%5d&i=0x1e+0x91+0x1c+0x83
Also, I need to cover all of this kind of chracters, not a bunch of them.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
As MindSwipe suggests, you may use p{C} to match any control character.
But you do not need to add a lot of code to subtract some characters you might want to keep, use character class subtraction:
var output = Regex.Replace(YourTextVariable, @"[p{C}-[trn]]+", "");
This will match one or more control characters different from tab, carriage return and line feed.
Method 2
Before reading further, please take a look Ryszard Czech’s answer on how to do this without any of the superfluous code of adding newlines back
This can achieved by replacing every control character in your string, and luckily Regex has the answer:
var s = "a nb" + Convert.ToString((char)0x1b) + Convert.ToString((char) 0x1e);
Regex.Replace(s, @"p{C}+", String.Empty);
@"p{C}+" matches all control characters. Be warned, this will also match new lines (n), meaning your output won’t have any newlines as you can see in this example. If you want your newlines to be kept, you’ll have to first split your string into an array, and Regex.Replace on each line, and the put them together again. Something like so:
var lines = s.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var sb = new StringBuilder();
foreach (var line in lines)
{
sb.AppendLine(Regex.Replace(line, @"p{C}+", String.Empty));
}
s = sb.ToString();
This leaves a trailing newline, which can easily be removed like so:
if (sb[sb.Length - 1] == 'n')
sb.Remove(sb.Length - 1, 1);
Do this before calling sb.ToString(). Here is a dotnetfiddle demonstrating this
Method 3
Sometimes a good old foreach is the right way to go. How about:
private static readonly char[] CharsToReplace =
{
'x02',
'x03',
'x04',
'x05',
'x06',
'x07',
'x08',
'x0B',
'x0C',
'x0E',
'x0F',
'x10',
'x11',
'x12',
'x13',
'x14',
'x15',
'x16',
'x17',
'x18',
'x19',
'x1a',
'x1b',
'x1c',
'x1d',
'x1e',
'x84',
'x86',
'x87',
'x88',
'x89',
};
public static string ReplaceNonPrintables(string stringToProcess)
{
StringBuilder buf = new StringBuilder(stringToProcess.Length);
foreach (var c in stringToProcess)
{
if (!CharsToReplace.Contains(c))
{
buf.Append(c)
}
}
return buf.ToString();
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
