2017-05-08 70 views
0

我想從0x00刪除所有控制字符到0x1F。以編程方式刪除字符串中的控制字符

我想我會那樣做:

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, ""); 
} 

雙逃避打擊它。好。但怎麼做呢?

+1

[從刪除字符串中的隱藏人物]的可能的複製(http://stackoverflow.com/questions/15259275/removing-hidden-characters-from-within-strings ) – MethodMan

回答

4

你可以使用LINQ:

x = new string(x.Where(c => (int)c >= 0x1F).ToArray()); 
+0

我喜歡LINQ。但是,我學會了C#1.0和2.0,並且只是用老式的編程方式思考,所以我努力應用它。 但謝謝你的解決方案。 – tuxmania